[Leetcode] 94. Binary Tree Inorder Traversal
https://leetcode.com/problems/binary-tree-inorder-traversal/binary tree가 하나 주어졌을 때, 해당 트리의 inorder traversal의 결과를 구하는 문제
Example 1
- Input : root = [1,null,2,3]
- Output : [1,3,2]
Example 2
- Input : root = []
- Output : []
Example 3
- Input : root = [1]
- Output : [1]
Note
- inorder() 함수를 만들어 Recursive하게 해결
- (참고) preorder traversal
- (참고) postorder 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 inorderTraversal(self, root: TreeNode) -> List[int]:
res = []
def inorder(self, node: TreeNode) -> None:
if not node :
return
inorder(self, node.left)
res.append(node.val)
inorder(self, node.right)
inorder(self, root)
return res