Skip to content

Commit afd4712

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.7 MB (84.13%)
1 parent b58ea90 commit afd4712

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<p>Given the <code>root</code> of a binary tree, return <em>the inorder traversal of its nodes&#39; values</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">[1,3,2]</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
14+
</div>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">[4,2,6,5,7,1,3,9,8]</span></p>
22+
23+
<p><strong>Explanation:</strong></p>
24+
25+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
26+
</div>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
34+
</div>
35+
36+
<p><strong class="example">Example 4:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
49+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
50+
</ul>
51+
52+
<p>&nbsp;</p>
53+
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
# LEFT -> ROOT -> RIGHT
10+
# return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []
11+
12+
stack = []
13+
res = []
14+
15+
while root or stack:
16+
while root:
17+
stack.append(root) # keep adding root in stack.
18+
root = root.left # traverse LEFT nodes at the end
19+
20+
root = stack.pop()
21+
res.append(root.val) # add ROOT to result.
22+
23+
root = root.right # traverse RIGHT nodes at the end
24+
25+
return res

0 commit comments

Comments
 (0)