Invert Binary Tree

Invert Binary Tree

问题:

Invert a binary tree.

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

to

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

思路:

  简单的递归

我的代码:

public class Solution {
    public TreeNode invertTree(TreeNode root) {
        helper(root);
        return root;
    }
    public void helper(TreeNode root)
    {
        if(root == null)    return;
        TreeNode left = root.left;
        TreeNode right = root.right;
        root.left = right;
        root.right = left;
        helper(left);
        helper(right);
    }
}
View Code

 

posted on 2015-06-16 09:46  zhouzhou0615  阅读(124)  评论(0编辑  收藏  举报

导航