Leetcode-226题:Invert Binary Tree

题目:
Invert a binary tree.
   4
  /  
 2   7
/ \  / 
1 3 6  9

to

4
  /  
 7   2
/ \  / 
9 6 3  1

代码:递归处理即可

def invertTree(self, root):
    """
    :type root: TreeNode
    :rtype: TreeNode
    """
    if root == None:
        return None
    root.left,root.right = root.right,root.left
    self.invertTree(root.left)
    self.invertTree(root.right)
    return root

你可能感兴趣的:(Leetcode-226题:Invert Binary Tree)