[leetcode-python3] 94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal - python3

Given the root of a binary tree, return the inorder traversal of its nodes' values.

My Answer 1: Accepted (Runtime: 24 ms - 94.91% / Memory Usage: 14.3 MB - 26.94%)

# 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]:
        result = []
        self.inorder(root, result)
        return result
        
    def inorder(self, root: TreeNode, result: List[int]) -> List[int]:
        if root is None:
            pass
        else:
            self.inorder(root.left, result)
            result.append(root.val)
            self.inorder(root.right, result)

전에 공부했던 in-order 순회를 생각하면서..
왼쪽 -> 루트 -> 오른쪽 순으로 재귀 돌면서 result 에 값 넣기

좋은 웹페이지 즐겨찾기