LintCode2016年8月22日算法比赛----克隆二叉树

克隆二叉树

题目描述

深度复制一个二叉树。
给定一个二叉树,返回一个它的克隆品。

样例

给定一个二叉树:

1
/
2 3
/
4 5
返回其相同结构相同数值的克隆二叉树:

1
/
2 3
/
4 5

Java算法实现

public class Solution {
/**
* @param root: The root of binary tree
* @return root of new tree
*/
public TreeNode cloneTree(TreeNode root) {
// Write your code here
if(root==null){
return null;
}
else{
TreeNode res=new TreeNode(root.val);
res.left=cloneTree(root.left);
res.right=cloneTree(root.right);
return res;
}
}
}

这题标记为Easy,确实比较Easy

posted on 2016-08-22 11:52  HorseShoe2016  阅读(244)  评论(0)    收藏  举报