Invert Binary Tree

Problem: Invert Binary Tree

In this problem, we can invert left and right children at current node and do the same thing to left and right children.

Code in Python:

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if not root: return None
        stack = [root]
        while stack:
            node = stack.pop()
            node.left, node.right = node.right, node.left
            if node.left: stack.append(node.left)
            if node.right: stack.append(node.right)
        return root

results matching ""

    No results matching ""