Lintcode375 Clone Binary Tree solution 题解
【题目描述】
For the given binary tree, return a deep copy of it.
深度复制一个二叉树,给定一个二叉树,返回一个他的克隆品。
【题目链接】
www.lintcode.com/en/problem/clone-binary-tree/
【题目解析】
假设有如下链表:
|---------------|
| v
1 --> 2 --> 3 --> 4
节点1的random指向了3。首先我们可以通过next遍历链表,依次拷贝节点,并将其添加到原节点后面,如下:
|-----------------------------|
| v
1 --> 1' --> 2 --> 2' --> 3 --> 3' --> 4 --> 4'
| ^
|----------------------|
因为我们只是简单的复制了random指针,所以新的节点的random指向的仍然是老的节点,譬如上面的1和1'都是指向的3。
调整新的节点的random指针,对于上面例子来说,我们需要将1'的random指向3',其实也就是原先random指针的next节点。
|------------------------------|
| v
1 --> 1' --> 2 --> 2' --> 3 --> 3' --> 4 --> 4'
| ^
|----------------------------|
最后,拆分链表,就可以得到深拷贝的链表了。
【参考答案】
Lintcode375 Clone Binary Tree solution 题解的更多相关文章
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- Leetcode ——Lowest Common Ancestor of a Binary Tree
Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. ...
- Leetcode-Construct Binary Tree from inorder and postorder travesal
Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)
[Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...
随机推荐
- 笔记:Maven 插件配置 - maven-jar-plugin
在项目中内部重用某个模块的测试代码很常见的需求,可能某个底层模块的测试代码中包含一些常用的测试工具类,或者一些高质量的测试基类供继承,这个时候Maven用户就需要通过配置此插件将测试类打包,插件配置代 ...
- 使用TensorFlow实现回归预测
这一节使用TF搭建一个简单的神经网络用于回归预测,首先随机生成一组数据 import tensorflow as tf import numpy as np import matplotlib.pyp ...
- redis-cli的一些有趣也很有用的功能
redis-cli我们最常用的两个参数就是-h.-p.-a选项,分配用来指定连接的redis-server的host和port. 通过redis-cli –help发现,redis-cli还提供了其他 ...
- 数据库 --> MySQL存储引擎介绍
MySQL存储引擎介绍 MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然 ...
- C#编程语言之委托与事件(二)—— C#事件
前面已经大致讲述了C#委托的一些基础知识点,本文接下来的内容是C#中的事件(Event),在此我提个建议,如果是刚接触C#的委托类型的朋友可以先看到这里,等熟悉了委托的使用之后(大约1-2天)再来了解 ...
- WebSocket 聊天室加自制服务器
自动监听本地ip 占用端口9930 打开服务器 再打开页面 输入服务器监听的ip和端口 局域网可以输入内网ip 外网连接 要输入服务器的外网ip 路由器需做好映射 实现WebSocket通信功能 和 ...
- 20155303 2016-2017-2 《Java程序设计》第二周学习总结
20155303 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 『注意』 "//"为单行批注符: "/*"与&quo ...
- django搭建web (一)
建立工程 django-admin.py startproject project_name 建立app python manage.py startapp app_name 将app添加进工程中 在 ...
- bzoj千题计划276:bzoj4515: [Sdoi2016]游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=4515 把lca带进式子,得到新的式子 然后就是 维护树上一次函数取min 一个调了一下午的错误: 当 ...
- windows系统下安装 node.js (node.js安装及环境配置)
node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...