13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed in data structure. The Node data structure contains two pointers to other Nodes.

在这道题让我们通过一个节点指针来复制整个数据结构,节点类Node中包含两个节点指针,我们需要用哈希表来建立原数据结构中每一个节点的地址到相对应的新结构中的地址,这样我们就可以在用DFS的时候知道哪些节点我们已经拷贝过了,就可以直接跳过。通过这种方式来标记访问过的节点可以不用在节点内部存储。拷贝过程如下所示:

class Node {
public:
Node *ptr1;
Node *ptr2;
}; typedef unordered_map<Node*, Node*> NodeMap; class Solution {
public:
Node* copy_structure(Node *root) {
NodeMap m;
return copy_recursive(root, m);
}
Node* copy_recursive(Node *cur, NodeMap &m) {
if (cur == nullptr) return nullptr;
auto it = m.find(cur);
if (it != m.end()) return it->second;
Node *node = new Node;
m[cur] = node;
node->ptr1 = copy_recursive(cur->ptr1, m);
node->ptr2 = copy_recursive(cur->ptr2, m);
return node;
}
};

[CareerCup] 13.7 Node Pointer 节点指针的更多相关文章

  1. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  2. [leetcode-117]填充每个节点的下一个右侧节点指针 II

    (1 AC) 填充每个节点的下一个右侧节点指针 I是完美二叉树.这个是任意二叉树 给定一个二叉树 struct Node { int val; Node *left; Node *right; Nod ...

  3. [LeetCode] 116. 填充每个节点的下一个右侧节点指针

    题目链接 : https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 题目描述: 给定一个完美二叉树 ...

  4. Java实现 LeetCode 117 填充每个节点的下一个右侧节点指针 II(二)

    117. 填充每个节点的下一个右侧节点指针 II 给定一个二叉树 struct Node { int val; Node *left; Node *right; Node *next; } 填充它的每 ...

  5. Java实现 LeetCode 116 填充每个节点的下一个右侧节点指针

    116. 填充每个节点的下一个右侧节点指针 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点.二叉树定义如下: struct Node { int val; Node *left ...

  6. C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>

    一.概念介绍 unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了独占式拥有的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有, ...

  7. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

  8. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

随机推荐

  1. Eclipse 导入项目后启动报异常:java.lang.UnsatisfiedLinkError: Native Library *.dll already loaded in another classloade 解决方法

    tomcat 服务器的配置信息如下:

  2. 监听spring加载完成后事件

    有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道hibernate有个二级缓存.没有用过memcache,也没有使用过redis. 只懂得将数据放到数组里或者集合里,一直不去销毁它(只有 ...

  3. Cron 表达式详解和案例

    1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...

  4. SAM4E单片机之旅——17、通过UART进行标准IO

    交互还是很有必要的,而且使用键盘和显示器的交互效率还是很高的.当然,可以直接使用UART进行字符的输入和输出.但是又何必浪费了C的标准输入输出的格式控制之类的功能呢? 这次内容就是使用scanf() ...

  5. 如何使用GOOGLE高级搜索技巧

    如何使用GOOGLE高级搜索技巧 一,GOOGLE简介 Google(www.google.com)是一个搜索引擎,由两个斯坦福大学博士生Larry Page与Sergey Brin于1998年9月发 ...

  6. Android 开发之 Android 开发的起步

    前言  Android 开发的起步 我们可以先来看看百科上面怎么说? 百度百科上 Android的介绍 一.Windows环境下在线搭建Android环境. 1. 下载 Android开发工具. JD ...

  7. 烂泥:KVM与kickstart集成

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. KVM与kickstart集成在这里我要说明下,因为在前面有关CentOS无人值守安装的文章中,我提到过如果要使用网卡PXE功能的话,内网中必须有DHC ...

  8. poj 3694 Network 边双连通+LCA

    题目链接:http://poj.org/problem?id=3694 题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数. 解题 ...

  9. mysql大小写问题

    以前做企业项目的时候,用的都是oracle数据库,在新公司项目用的是mysql,有关mysql大小写的问题 1   windows下默认mysql是不区分大小写的,要想让其支持大小写.更改方法 在my ...

  10. 探索 OpenStack 之(10):深入镜像服务Glance

    本篇博文来探讨下镜像服务Glance. 0.  基本概念 0.1 基本功能 Glance提供REST API来支持以下镜像操作: 查询 注册 上传 获取 删除 访问权限管理 0.2 Glance RE ...