https://algorithms.tutorialhorizon.com/construct-a-binary-tree-from-given-inorder-and-level-order-traversal/
https://www.geeksforgeeks.org/construct-tree-inorder-level-order-traversals/
http://javabypatel.blogspot.com/2015/08/construct-binary-tree-from-inorder-and-level-order-traversals.html
https://www.cnblogs.com/linyx/p/4085784.html
- First element in the levelorder [] will be the root of the tree, here it is 1.
- Now the search element 1 in inorder[], say you find it at position i, once you find it, make note of elements which are left to i (this will construct the leftsubtree) and elements which are right to i ( this will construct the rightSubtree).
- Suppose in previous step, there are X number of elements which are left of ‘i’ (which will construct the leftsubtree), but these X elements will not be in the consecutive in levelorder[] so we will extract these elements from levelorder[] by maintaining their sequence and store it in an array say newLeftLevel[].
- Similarly if there are Y number of elements which are right of ‘i’ (which will construct the rightsubtree), but these Y elements will not be in the consecutive in levelorder[] so we will extract these elements from levelorder[] by maintaining their sequence and store it in an array say newRightLevel[].
- From previous two steps construct the left and right subtree and link it to root.left and root.right respectively by making recursive calls using newLeftLevel[] and newRightLevel[].
- See the picture for better explanation.
public Node makeBTree(int[] inorder, int[] levelOrder, int iStart, int iEnd) {
if (iStart > iEnd) {
return null;
}
int rootVal = levelOrder[0];
Node root = new Node(rootVal);
if (iStart == iEnd) {
return root;
}
int index = findIndex(inorder, rootVal, iStart, iEnd);
int[] newleftLevel = newLevelOrder(inorder, levelOrder, iStart,
index - 1);
int[] newrighttLevel = newLevelOrder(inorder, levelOrder, index + 1,
iEnd);
root.left = makeBTree(inorder, newleftLevel, iStart, index - 1);
root.right = makeBTree(inorder, newrighttLevel, index + 1, iEnd);
return root;
}
public int[] newLevelOrder(int[] inorder, int[] levelOrder, int iStart,
int iEnd) {
int[] newlevel = new int[iEnd - iStart + 1];
int x = 0;
for (int i = 0; i < levelOrder.length; i++) {
if (findIndex(inorder, levelOrder[i], iStart, iEnd) != -1) {
newlevel[x] = levelOrder[i];
x++;
}
}
return newlevel;
}
public int findIndex(int[] inorder, int value, int iStart, int iEnd) {
int x = -1;
for (int i = iStart; i <= iEnd; i++) {
if (value == inorder[i]) {
x = i;
}
}
return x;
}
An upper bound on time complexity of above method is O(n3). In the main recursive function, extractNodes() is called which takes O(n2) time.
X.
Approach : Following algorithm uses O(N^2) time complexity to solve the above problem using the unordered_set data structure in c++ (basically making a hash-table) to put the values of left subtree of the current root and later and we will check in O(1) complexity to find if the current levelOrder node is part of left subtree or not.
If it is the part of left subtree then add in one lLevel arrray for left other wise add it to rLevel array for right subtree.
https://www.cnblogs.com/linyx/p/4085784.html