Skip to content

Commit b88a367

Browse files
My solution for 145
1 parent b20ea40 commit b88a367

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

problems/145/jeremymanning.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
# [Problem 145: Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4+
- A nice easy one today! This is just a depth-first search
45

56
## Refining the problem, round 2 thoughts
7+
- Nothing too tricky here. The order should be "left, right, root"
8+
- I think we can just return the visited notes in reverse order
69

710
## Attempted solution(s)
811
```python
9-
class Solution: # paste your code here!
10-
...
12+
# Definition for a binary tree node.
13+
# class TreeNode:
14+
# def __init__(self, val=0, left=None, right=None):
15+
# self.val = val
16+
# self.left = left
17+
# self.right = right
18+
class Solution:
19+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
20+
vals = []
21+
stack = [root]
22+
while stack:
23+
node = stack.pop()
24+
if node is not None:
25+
vals.append(node.val)
26+
stack.extend([node.left, node.right])
27+
return vals[::-1]
1128
```
29+
- Given test cases pass
30+
- New test case:
31+
- `root = [1,null,2,3, 4, 5, 6, 7, null, 8, null, 9, 10, -100, -100]`: pass
32+
- Let's submit...
33+
34+
![Screenshot 2024-08-24 at 8 59 51 PM](https://github.com/user-attachments/assets/ace94bdb-eab7-409d-a465-469f71e73edb)
35+
36+
- Solved!

0 commit comments

Comments
 (0)