Skip to content

Commit d81f21e

Browse files
committed
Sync LeetCode submission Runtime - 22 ms (46.63%), Memory - 18.2 MB (96.19%)
1 parent 34c3cb5 commit d81f21e

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>We run a&nbsp;preorder&nbsp;depth-first search (DFS) on the <code>root</code> of a binary tree.</p>
2+
3+
<p>At each node in this traversal, we output <code>D</code> dashes (where <code>D</code> is the depth of this node), then we output the value of this node.&nbsp; If the depth of a node is <code>D</code>, the depth of its immediate child is <code>D + 1</code>.&nbsp; The depth of the <code>root</code> node is <code>0</code>.</p>
4+
5+
<p>If a node has only one child, that child is guaranteed to be <strong>the left child</strong>.</p>
6+
7+
<p>Given the output <code>traversal</code> of this traversal, recover the tree and return <em>its</em> <code>root</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex1.png" style="width: 423px; height: 200px;" />
12+
<pre>
13+
<strong>Input:</strong> traversal = &quot;1-2--3--4-5--6--7&quot;
14+
<strong>Output:</strong> [1,2,5,3,4,6,7]
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex2.png" style="width: 432px; height: 250px;" />
19+
<pre>
20+
<strong>Input:</strong> traversal = &quot;1-2--3---4-5--6---7&quot;
21+
<strong>Output:</strong> [1,2,5,3,null,6,null,4,null,7]
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex3.png" style="width: 305px; height: 250px;" />
26+
<pre>
27+
<strong>Input:</strong> traversal = &quot;1-401--349---90--88&quot;
28+
<strong>Output:</strong> [1,401,null,349,88,90]
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li>The number of nodes in the original tree is in the range <code>[1, 1000]</code>.</li>
36+
<li><code>1 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
37+
</ul>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Approach 2: Iterative Approach with Stack (Single Pass)
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
13+
class Solution:
14+
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
15+
stack = []
16+
index = 0
17+
18+
while index < len(traversal):
19+
# Count no. of dashes
20+
depth = 0
21+
while index < len(traversal) and traversal[index] == '-':
22+
depth += 1
23+
index += 1
24+
25+
# Node value
26+
value = 0
27+
while index < len(traversal) and traversal[index].isdigit():
28+
value = value * 10 + int(traversal[index])
29+
index += 1
30+
31+
node = TreeNode(value)
32+
33+
while len(stack) > depth:
34+
stack.pop()
35+
36+
if stack:
37+
if stack[-1].left is None:
38+
stack[-1].left = node
39+
else:
40+
stack[-1].right = node
41+
42+
stack.append(node)
43+
44+
return stack[0]
45+

0 commit comments

Comments
 (0)