Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 464 Bytes

226_Invert_Binary_Tree.md

File metadata and controls

32 lines (27 loc) · 464 Bytes
  1. Invert Binary Tree

Invert a binary tree.

Example:

Input:

     4
   /   \
  2     7
 / \   / \
1   3 6   9
Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root == None:
            return
        else:
            root.left, root.right = root.right, root.left
            self.invertTree(root.left)
            self.invertTree(root.right)
        return root