You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a binary tree from inorder and preorder traversal.
Given inorder and preorder traversal notation of a binary tree. Create the tree from it
Describe the solution you'd like
The first element of preorder array will be the root of binary tree. So create root from it
In inorder traversal all the elements towards left of an index-i forms left subtree for tree rooted at 'i' and right part of array forms the right subtree.
Hence, find the index in inorder representation for the first element of preorder array. Say it occurs at i-th index.
Traverse those many elements in preorder array and use that part of preorder array and inorder[0...i-1] to create left subtree
Similary use inorder[i+1...] and rest of elements in preorder array to create right subtree.