Recover a Tree From Preorder Traversal
PreviousMaximum Product of Splitted Binary TreeNextReverse alternate levels of a perfect binary tree
Last updated
Last updated
We run a preorder depth first search on the root
of a binary tree.
At each node in this traversal, we output D
dashes (where D
is the depth of this node), then we output the value of this node. (If the depth of a node is D
, the depth of its immediate child is D+1
. The depth of the root node is 0
.)
If a node has only one child, that child is guaranteed to be the left child.
Given the output S
of this traversal, recover the tree and return its root
.
Example 1:
Example 2:
Example 3:
Note:
The number of nodes in the original tree is between 1
and 1000
.
Each node will have a value between 1
and 10^9
.