Description

For the given binary tree, return a deep copy of it.

Example

Given a binary tree:

     1
/ \
2 3
/ \
4 5

return the new binary tree with same structure and same value:

     1
/ \
2 3
/ \
4 5

解题:链表复制。递归解法比较简单,代码如下:

/**
* 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) {
// write your code here
if(root == null){
return null;
}
TreeNode new_root = new TreeNode(root.val);
new_root.left = cloneTree(root.left);
new_root.right = cloneTree(root.right);
return new_root;
}
}

最新文章

  1. 连锁机构3D指纹考勤系统解决方案
  2. LINQ 联表查询 取count 值
  3. 2016-11-02: boost::enable_shared_from_this
  4. Convert Sorted Array to Binary Search Tree With Minimal Height
  5. Nvidia VertexTextureFetch Water
  6. C puzzles详解【9-12题】
  7. 实例化新的一个(new)
  8. H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验
  9. Linux下find指令
  10. 如何在Objective-C中实现链式语法?
  11. UVA - 10129Play on Words(欧拉路)
  12. 用JDBC把Excel中的数据导入到Mysql数据库中
  13. CSS3-1
  14. Mysql 函数大全- 5.6 中文解释函数参考
  15. 如何在Delphi 中使用 DevExpressVCL的 CxGrid与CxTreeList,编辑某列后计算另一列的值
  16. 谈谈MySQL死锁之二 死锁检测和处理源码分析
  17. Selenium+Python自动化测试环境搭建和搭建过程遇到的问题解决
  18. WebSocket【转】
  19. ArcGIS API for Silverlight 的重要内容******重要
  20. 【树哈希】poj1635 Subway tree systems

热门文章

  1. P2894 [USACO08FEB]酒店Hotel
  2. “->”和“.”运算符
  3. 【译】2分钟介绍Rx
  4. Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践
  5. python统计文档中词频
  6. Oracle查找lobsegment、lobindex对应的表
  7. textarea中输入字数的限制
  8. kendo UI 倒如css 和 js 后 窗口控件上的工具栏图标不显示如何解决
  9. javascript中的栈堆内存
  10. CF考古活动