13.7 写一个函数,其中一个参数是指向Node结构的指针,返回传入数据结构的一份完全拷贝。 Node结构包含两个指针,指向另外两个Node。

C++实现代码:

typedef map<Node*, Node*> NodeMap;
Node* copy_recursive(Node *cur, NodeMap &nodeMap){
if(cur == NULL){
return NULL;
}
NodeMap::iterator i = nodeMap.find(cur);
if (i != nodeMap.end()){
// we’ve been here before, return the copy
return i->second;
}
Node *node = new Node;
nodeMap[cur] = node; // map current node before traversing links
node->ptr1 = copy_recursive(cur->ptr1, nodeMap);
node->ptr2 = copy_recursive(cur->ptr2, nodeMap);
return node;
}
Node* copy_structure(Node* root){
NodeMap nodeMap; // we will need an empty map
return copy_recursive(root, nodeMap);
}

careercup-C和C++ 13.7的更多相关文章

  1. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  4. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  5. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  6. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

  7. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  8. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  9. [CareerCup] 13.7 Node Pointer 节点指针

    13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...

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

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

随机推荐

  1. [转] web.xml文件详解

    转自:http://www.cnblogs.com/hellojava/archive/2012/12/28/2835730.html 前言:一般的web工程中都会用到web.xml,web.xml主 ...

  2. acdream LCM Challenge (最小公倍数)

    LCM Challenge Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) Su ...

  3. ↗☻【编写可维护的JavaScript #BOOK#】第4章 变量、函数和运算符

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  4. SQL RIGHT JOIN 关键字

    SQL RIGHT JOIN 关键字 RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行. RIGHT JOIN ...

  5. java.lang.Thread.State类详解

    public static enum Thread.Stateextends Enum<Thread.State>线程状态.线程可以处于下列状态之一: 1.NEW 至今尚未启动的线程的状态 ...

  6. 【转】使用NDK生成native C/C++的可执行程序

    原文网址:http://www.linuxidc.com/Linux/2011-08/40901.htm 众所周知, NDK可以生成lib,让java程序通过jni来调用,其实,NDK也可以生成C/C ...

  7. 【转】tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  8. GCC 编译命令

    今天突然被同事问道一个GCC编译命令为的问题,感觉对相应内容生疏了,赶紧整理下相关内容,梳理下相关知识. GCC命令提供了非常多的命令选项,但并不是所有都要熟悉,初学时掌握几个常用的就可以了,到后面再 ...

  9. PHP基本语法的小结

    一.PHP能做什么? PHP能做什么?我觉得它很强大,只要我能想到的,它都能做,只是我技术能力还不行╮(╯﹏╰)╭.好吧,一张图,基本了解一下吧(ps:PHP的功能不局限于此( ^_^ )) 图像有点 ...

  10. 清空具有外键约束的表时报ERROR 1701(42000)的解决办法

    ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`furion`.`tbl_fr ...