[Leetcode] 94. Binary Tree Inorder Traversal

서해빈·2021년 3월 29일
0

코딩테스트

목록 보기
32/65

문제 바로가기

Time Complexity: O(n)O(n)
Space Complexity: O(n)O(n)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        def traversal(node: TreeNode, trav: List[int]) -> None:
            # node not exist
            if not node:
                return
            # inorder: left child -> parent -> right child
            if node.left:
                traversal(node.left, trav)
            trav.append(node.val)
            if node.right:
                traversal(node.right, trav)
                
        res = list()
        traversal(root, res)
        return res
    

1개의 댓글

comment-user-thumbnail
2021년 12월 23일

It is a best solution found that very popular and helpful
https://www.youtube.com/watch?v=WHUMCUuktJk&ab_channel=EricProgramming

답글 달기