Skip to content

Commit b58ea90

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.9 MB (34.13%)
1 parent e33b31c commit b58ea90

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-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&nbsp;binary tree, return <em>the postorder 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">[3,2,1]</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,6,7,5,2,9,8,3,1]</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 the 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
# left -> right -> root.
10+
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root else []
11+

0 commit comments

Comments
 (0)