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
When preorder, and inorder values are given, where each element is node's value, contruct binary tree.
Each element in the preorder and inorder is unique.
Approach
Preorder is traversal that move head first then move to left, right.
On the other hand, Inorder is traversal that move left first then move to head, right.
We can get the head element of the tree by picking first item of preorder.
Then, we can divide inorder into left side and right side, because we know what head value is.
Finally, we can also divide preorder, because we know the length of the left-side and the length of the right-side.
Problem
When preorder, and inorder values are given, where each element is node's value, contruct binary tree.
Each element in the preorder and inorder is unique.
Approach
Preorder is traversal that move head first then move to left, right.
On the other hand, Inorder is traversal that move left first then move to head, right.
We can get the head element of the tree by picking first item of preorder.
Then, we can divide inorder into left side and right side, because we know what head value is.
Finally, we can also divide preorder, because we know the length of the left-side and the length of the right-side.
The code is below.
The text was updated successfully, but these errors were encountered: