Chloe Jungah Kim
Chloe Jungah Kim
A blogger who writes about everything.

[Leetcode] 226. Invert Binary Tree

https://leetcode.com/problems/invert-binary-tree/
[Leetcode] 226. Invert Binary Tree

binary tree가 주어졌을 때, 이를 좌우로 뒤집는 문제

Example 1

  • Input : root = [4,2,7,1,3,6,9]
  • Output : [4,7,2,9,6,3,1]

Example 2

  • Input : root = [2,1,3]
  • Output : [2,3,1]

Example 3

  • Input : root = []
  • Output : []

Note

  • invert() 함수를 만들어서 해당 노드의 자식 노드 두 개를 바꾸어주는 방식으로 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if not root :
            return root
        def invert(self, node: TreeNode):
            temp = node.right
            node.right = node.left
            node.left = temp
            if node.right :
                invert(self, node.right)
            if node.left :
                invert(self, node.left)
        invert(self, root)
        return root