Saturday, March 31, 2012

Construct binary tree from inorder and preorder.


In Binary tree .. from the in-order traversal and pre-order travrsal .. construct the tree.


Input:
int [] inorder = {5, 9, 7, 8, 2, 10, 3};
int [] preorder = {8, 9, 5, 7, 10, 2, 3};


output:
printing inorder of the built binary tree: 
5 9 7 8 2 10 3 


Approach:
1. First data of preorder Array is 8, so it would be root of the binary tree.
2. All the elements in inorder array before 8 would be the left subtree and all the elements to the right of 8 in inorder array would be right subtree.
3. develope the recursion based on 1. and 2.

No comments:

Post a Comment