[Leetcode] 145. Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/binary tree가 하나 주어졌을 때, 해당 트리의 postorder traversal의 결과를 구하는 문제
Example 1
- Input : root = [1,null,2,3]
- Output : [3,2,1]
Example 2
- Input : root = []
- Output : []
Example 3
- Input : root = [1]
- Output : [1]
Note
- postorder() 함수를 만들어 Recursive하게 해결
- (참고) inorder traversal
- (참고) preorder traversal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
res = []
def postorder(self, node: TreeNode) -> None:
if not node :
return
postorder(self, node.left)
postorder(self, node.right)
res.append(node.val)
postorder(self, root)
return res