Invert Binary Tree

问题描述:

    4
   /   \
  2     7      
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1

递归反转:
先反转根节点的左子树和右子树 然后再分别反转左子树,右子树各自的下的左子树右子树。依此类推;
代码:
public static TreeNode invertTree(TreeNode root) {
        if(root==null) return null;
            TreeNode tmp=root.left;
            root.left=root.right;
            root.right=tmp;     
            invertTree(root.left);
            invertTree(root.right);
        return root;
    }

 

posted @ 2015-06-24 12:05  hhrrrr  阅读(201)  评论(0)    收藏  举报