careercup-C和C++ 13.7
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的更多相关文章
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- [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 ...
- [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行,最 ...
- [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 ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- [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. 这道题问 ...
- [CareerCup] 13.5 Volatile Keyword 关键字volatile
13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...
- [CareerCup] 13.6 Virtual Destructor 虚析构函数
13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...
- [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 ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
随机推荐
- [HDU 4828] Grids
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- mysql qps tps
(1)QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...
- ORACLE SQLloader详细语法
Oracle SQL Loader的详细语法 SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据 ...
- InnoDB关键特性之insert buffer
insert buffer 是InnoDB存储引擎所独有的功能.通过insert buffer,InnoDB存储引擎可以大幅度提高数据库中非唯一辅助索引的插入性能. 数据库对于自增主键值的插入是顺序的 ...
- HDU 1495 非常可乐 BFS搜索
题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...
- FreeModbus 移植于STM32 实现Modbus RTU通信
http://ntn314.blog.163.com/blog/static/161743584201233084434579/ 毕业设计自己要做个基于STM32的PLC能直接跑语句表的,现在看来好像 ...
- Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)
结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...
- iOS语音播放之切换听筒和扬声器的方法解决方案
关于流媒体播放的相关知识可以加本人QQ:564702640 一起来讨论 [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; // ...
- UIImagePickerController Class 概述
不能定制界面,不可派生子类. 使用步骤: 检查制定源是否可用. isSourceTypeAvailable:方法. 检查可用媒体(视频还是只能是图片) availableMediaTypesFo ...
- 转载SSIS中的容器和数据流—数据转换(Transformations)
对数据流来说按照需求将数据转换成需要的格式是数据操作中的一个关键的步骤.例如想要得到聚合排序后的运算结果,转换可以实现这种操作.和SQL Server 2000 DTS完全不同,这些操作不需要编写sc ...