LeetCode/Top Interview 150

226. Invert Binary Tree

hyunkookim 2024. 12. 11. 17:41

226. Invert Binary Tree

 

https://youtu.be/OnSn2XEQ4MY?si=M8XrLJXb_NV2PHRw

 

정말 쉽고, 기본적인 문제라는데, .. 그리고, DFS 로.. , DFS 이면,, 재귀로..

 

# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root:
            return root

        tmp = root.left
        root.left = root.right
        root.right = tmp

        root.left = self.invertTree(root.left)
        root.right = self.invertTree(root.right)

        return root

'LeetCode > Top Interview 150' 카테고리의 다른 글

106. Construct Binary Tree from Inorder and Postorder Traversal  (0) 2024.12.13
101. Symmetric Tree  (0) 2024.12.11
Hashmap: 146. LRU Cache ★★★  (0) 2024.12.10
86. Partition List  (0) 2024.12.10
61. Rotate List  (0) 2024.12.10