diff --git a/Answer50.cpp b/Answer50.cpp new file mode 100644 index 0000000..6d405e2 --- /dev/null +++ b/Answer50.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; +class node +{ + public: + char data; + node* left; + node* right; +}; +int search(char arr[], int strt, int end, char value); +node* newNode(char data); +node* buildTree(char in[], char pre[], int inStrt, int inEnd) +{ + static int preIndex = 0; + + if (inStrt > inEnd) + return NULL; + node* tNode = newNode(pre[preIndex++]); + if (inStrt == inEnd) + return tNode; + int inIndex = search(in, inStrt, inEnd, tNode->data); + tNode->left = buildTree(in, pre, inStrt, inIndex - 1); + tNode->right = buildTree(in, pre, inIndex + 1, inEnd); + + return tNode; +} +int search(char arr[], int strt, int end, char value) +{ + int i; + for (i = strt; i <= end; i++) + { + if (arr[i] == value) + return i; + } +} +node* newNode(char data) +{ + node* Node = new node(); + Node->data = data; + Node->left = NULL; + Node->right = NULL; + + return (Node); +} +void printInorder(node* node) +{ + if (node == NULL) + return; + printInorder(node->left); + cout<data<<" "; + printInorder(node->right); +} + +/* Driver code */ +int main() +{ + char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' }; + char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' }; + int len = sizeof(in) / sizeof(in[0]); + node* root = buildTree(in, pre, 0, len - 1); + cout << "Inorder traversal of the constructed tree is \n"; + printInorder(root); +}