logo头像
Snippet 博客主题

【Leetcode 226】Invert Binary Tree

难度: 简单(Easy)

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

to

4
/  
7   2
/ \  / 
9 6 3  1

代码:递归处理即可

1
2
3
4
5
6
7
8
9
10
11
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

评论系统未开启,无法评论!