Given an array representing the level-order traversal of a binary tree (not BST), reconstruct the tree.
Example:
Input: [6,x,8,4,x]
output: 6
\
8
/
4
x in the array represents null nodes
Many existing solutions use 2i + 1 and 2i + 2 to access the left and right child, where i is the current node in the array. This solution only works if the input array represents a complete binary tree, which means the input in the example above has to be [6,x,8,x,x,4,x]. I need a solution that works with any input. Thank you!
Edit:
Assume the TreeNode class has the following structure:
class TreeNode{
String value;
TreeNode left;
TreeNode right;
}
Also, the output was just an illustration of what the tree should look like. But you can just return a TreeNode which is the root of the tree.
Could you do something like this?
So this:
Would output this: