I'm working on a coding question and right now I'm bit confused. If I'm given an array which represents level order traversal of a binary tree. How do I construct a tree from that?
So far here is my thought process so far: I know the 0th index is the root, leftChild = 2*i+1 and rightChild = 2*i+2.
Here is what I have so far and I don't think its right:
public Tree buildTree(ArrayList<Tree> arr, int i) {
if (i > list.size() - 1) {
return null;
}
root = list.get(i);
root.LeftChild = buildTree(arr, 2*i+1);
root.RightChild = buildTree(arr, 2*i+2);
return root;
}
my i starts at 0, Thanks.