[LeetCode] Binary Tree Inorder Traversal (Python)

2024. 3. 7. 10:28알고리즘

 

 

# 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: Optional[TreeNode]) -> List[int]:
        answer = []
        
        def dfs(node):
            if node is not None:
                dfs(node.left)
                answer.append(node.val)
                dfs(node.right)
                
        dfs(root)
        return answer