Fork me on GitHub

Lintcode 375.克隆二叉树

--------------------------

 

水题

 

AC代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree
     * @return root of new tree
     */
    public TreeNode cloneTree(TreeNode root) {
        if(root==null) return null;
        TreeNode copycat=new TreeNode(root.val);
        copycat.left=cloneTree(root.left);
        copycat.right=cloneTree(root.right);
        return copycat;
    }
}

 

题目来源: http://www.lintcode.com/zh-cn/problem/clone-binary-tree/#

 

posted @ 2017-01-01 23:14  CC11001100  阅读(176)  评论(0编辑  收藏  举报