Skip to content

Commit

Permalink
Create 0145-binary-tree-postorder-traversal.py
Browse files Browse the repository at this point in the history
  • Loading branch information
neetcode-gh authored Mar 19, 2023
1 parent 2dd06cd commit 3ae21e2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/0145-binary-tree-postorder-traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
stack = [root]
visit = [False]
res = []

while stack:
cur, v = stack.pop(), visit.pop()
if cur:
if v:
res.append(cur.val)
else:
stack.append(cur)
visit.append(True)
stack.append(cur.right)
visit.append(False)
stack.append(cur.left)
visit.append(False)
return res

0 comments on commit 3ae21e2

Please sign in to comment.