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