Problem
We run a preorder depth-first search (DFS) 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 traversal
of this traversal, recover the tree and return its root
.
Examples
Example 1:
graph TD; A("1") B("2") C("3") D("4") E("5") F("6") G("7") A --- B & E B --- C & D E --- F & G
|
|
Example 2:
graph TD; A("1") B("2") C("3") D("4") E("5") F("6") G("7") A --- B & E B --- C B ~~~ N1:::hidden C --- D C ~~~ N2:::hidden E --- F E ~~~ N3:::hidden F --- G F ~~~ N4:::hidden classDef hidden display: none;
|
|
Example 3:
graph TD; A("1") B("401") C("349") D("88") E("90") A --- B A ~~~ N1:::hidden B --- C & D C --- E C ~~~ N2:::hidden classDef hidden display: none;
|
|
Constraints:
- The number of nodes in the original tree is in the range
[1, 1000]
. 1 <= Node.val <= 10^9
Solution
Method 1 - Iterative using stack
Here is the approach:
- Parse the string to extract (depth, value) pair. This helps us extract numbers and their corresponding depths based on the count of dashes.
- Initialise an empty stack to keep track of nodes at each depth. This helps in identifying where to attach a node in the tree (as the child of which parent node).
- For each extracted (depth, value):
- Create a tree node with the given value.
- If the stack’s size corresponds to the depth, attach this node as a left child of the last node in the stack.
- Otherwise, pop nodes from the stack until the stack’s size matches the current depth, and then attach the node as the right child of the last node in the stack.
- Add the current node to the stack.
Video explanation
Here is the video explaining this method in detail. Please check it out:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the length of the string. Parsing the string and processing each node takesO(n)
. - 🧺 Space complexity:
O(h)
, , whereh
is the height of the tree. The maximum depth of the stack isO(h)