알고리즘/코딩 테스트

[Leetcode] Invert Binary Tree

gooooooood 2020. 6. 2. 19:03
반응형

solution for Invert Binary Tree

 

 

Problem)

Invert a binary tree.

 

 

Example)

 

Solution)

class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root:
            root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
            return root
        else:
            return None

 

 

Reference)

Recursion (Recursive Function)

반응형