【题目描述】

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'

|                                  ^

|----------------------------|

最后,拆分链表,就可以得到深拷贝的链表了。

【参考答案】

www.jiuzhang.com/solutions/clone-binary-tree/

Lintcode375 Clone Binary Tree solution 题解的更多相关文章

  1. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  2. 375. Clone Binary Tree【LintCode java】

    Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...

  3. 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. ...

  4. Leetcode-Construct Binary Tree from inorder and postorder travesal

    Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...

  5. 【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 ...

  6. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  7. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  8. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  9. 【题解】【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 ...

随机推荐

  1. 笔记:Struts2 的 JSON 插件

    安装插件,将其复制到Web应用的WEB-INF/lib 目录下 Struts2-json-plugin-2.3.16.3.jar json-lib-2.3-jdk15.jar commons-bean ...

  2. Quikapp快应用开发入门

    快应诞生背景 微信的小程序使得很多原来需要调动APP的场景不复存在,正式由于微信小程序的冲击,3月20日,华为联手九大手机厂商,共同举办了“快应用”标准启动发布会.“快应用”是几家手机厂商基于硬件平台 ...

  3. 理解python的元类

    看了一篇文档,借鉴一下!写下自己对python元类的理解,欢迎各位大神给出意见. 我的理解就是 type用来创建元类,元类用来创建类,类用来创建实例 这样一想,是不是可以认为元类创建类的过程等同于类创 ...

  4. mongodb的安装和配置

    1.下载安装 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz tar zxf mongodb-linux-x8 ...

  5. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

  6. Cloesest Common Ancestors

    Cloesest Common Ancestors 题目大意:给出一个n个节点的树,m组询问求两点LCA. 注释:n<=900. 想法:这题一看,我去,这不傻题吗?一看读入方式,完了,懵逼了.. ...

  7. vs运行单个cpp文件

    打开vs,新建项目,左侧win32见上图,右侧 win32控制台应用程序,填好名称后,确定----下一步,如下图,空项目 紧接着如下图,通过现有项添加自己的cpp文件,便可以运行了

  8. @Cacheable的实现原理

    如果你用过Spring Cache,你一定对这种配置和代码不陌生: <cache:annotation-driven cache-manager="cacheManager" ...

  9. 关于伪类after后续追加,实现js事件(如点击事件)

    实现情况为:点击"编辑"后,"编辑"文字变成"完成",再点击伪类元素后的"完成",此时的"完成"应该 ...

  10. 【Alpha】咸鱼冲刺日记第一天-黄紫仪

    总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 emmmmm,事实上它还没有正式开始.所以依旧没有[突然觉得明天任务真重] 三,项目进展 emmmmm,我错了咸鱼了两天才突然反应过来 ...