【leetcode】226. Invert Binary Tree
题目如下:
解题思路:“你再牛逼有什么用,写不出二叉树反转就是不行。” 所以,我们一定要会写。方法是递归,递归,递归,递归交换节点的左右节点。
代码如下:
# 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 traverse(self,node): tmp = node.left node.left = node.right node.right = tmp if node.left != None: self.invertTree(node.left) if node.right != None: self.invertTree(node.right) def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root == None: return root self.traverse(root) return root