Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Answer50.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
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<<node->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);
}