Skip to content

[226] Invert Binary Tree

https://leetcode.com/problems/invert-binary-tree/description/

  • algorithms
  • Easy (55.83%)
  • Source Code: 226.invert-binary-tree.py
  • Total Accepted: 314.6K
  • Total Submissions: 545.2K
  • Testcase Example: '[4,2,7,1,3,6,9]'

Invert a binary tree.

Example:

Input:

 4

/
2 7 / \ /
1 3 6 9

Output:

 4

/
7 2 / \ /
9 6 3 1

Trivia: This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.

python
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if not root: return None

        root.left, root.right = root.right, root.left

        self.invertTree(root.left)
        self.invertTree(root.right)
        return root
#
# n1 = TreeNode(4)
# n2 = TreeNode(2)
# n3 = TreeNode(7)
# n4 = TreeNode(1)
# n5 = TreeNode(3)
# n6 = TreeNode(6)
# n7 = TreeNode(9)
#
# n1.left, n1.right = n2, n3
# n2.left, n2.right = n4, n5
# n3.left, n3.right = n6, n7
#
#
# s = Solution()
# s.invertTree(n1)

Last updated: