美文网首页
Leetcode-226题:Invert Binary Tree

Leetcode-226题:Invert Binary Tree

作者: 八刀一闪 | 来源:发表于2016-10-08 22:12 被阅读4次

题目:
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

      本文链接:https://www.haomeiwen.com/subject/dhcpyttx.html