00:01
Hello student to reconstruct a binary tree based on its preorder and inorder traversals so we can use the following algorithm.
00:11
So here steps are number one start with the first node in the preorder traversal so this is the root.
00:17
So first we will look for the preorder and it will give us the root.
00:21
Number two find the index of the root node with the help of inorder traversal.
00:26
So this index divides the inorder traversal into two subarrays, so here left sub tree and right sub tree.
00:37
So by using the inorder traversal recursively construct the left sub tree using the preorder traversals and the left sub array using the inorder traversal.
00:46
Recursively reconstruct the right sub tree using the preorder traversals and right sub array of the inorder traversals.
00:54
So here example preorder traversals is given so this is the preorder traversals and inorder traversal is also given so with the help by using both two we will now construct the binary tree.
01:09
So here first preorder traversal so it is clear that root node will be here 12 and now left subtree will be 5 to 9 and the right subtree will be 18, 15, 3, 18.
01:23
How we came to know? with the help of this this is a root so 12 and this is the left subtree and remaining these will be the right.
01:38
So left and right we find out with the help of inorder and with the help of preorder we find the root.
01:46
Now root node is 12 left is this one right subtree is this one now reconstruct the left subtree.
01:54
So now we have to reconstruct the left subtree.
01:57
So here preorder traversal is 5 to 9 and inorder traversal is 2, 5, 9.
02:05
So it is very clear that root node is 5 and left subtree will be 2 and the right subtree will be 9.
02:11
So first we find the root and after that we find for the left.
02:17
So here with the help of inorder preorder we find the root and left and right.
02:24
So now reconstruct the right subtree...