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 3549 Flow Problem 流问题(最大流,入门)
题意:给个赤裸的最大流问题. 思路:EK+BFS解决.跟HDU1532几乎一样的. #include <bits/stdc++.h> #define LL long long #defin ...
- 利用icepdf将pdf文件转为图片
所需jar 包为icepdf-core.jar.icepdf-extra.jar.icepdf-pro-intl.jar.icepdf-pro.jar和icepdf-viewer.jar. 示例代码如 ...
- 【转】报错:Program "sh" not found in PATH
原文网址:http://www.cnblogs.com/SadNight/p/3406201.html (1) 报错:Program "sh" not found in PATH ...
- List<HashMap>和HashMap
例如select查询出的是学号.姓名,比如查出符合条件的是学号是0810的小红,学号是0811的小明,组织起来如下: list.add(hashmap1);list.add(hashmap2); ...
- ArcGIS 读写lyr层文件
原文arcengine C# 读写lyr(转) 写lyr IFeatureLayer LineLayer = axMapControl1.get_Layer() as IFeatureLayer; I ...
- 淘宝JAVA中间件Diamond详解(一)---简介&快速使用
大家好,今天开始为大家带来我们通用产品团队的产品 —— diamond的专题,本次为大家介绍diamond的概况和快速使用. 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是 ...
- HDU 1024 Max Sum Plus Plus 简单DP
这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...
- MATLAB绘图与图形处理
参考:http://www.cnblogs.com/djcsch2001/tag/MATLAB/ matlab部分写的不错! 7.2 三维图形 7.2.1 三维曲线.面填色命令 命令1 com ...
- C# Asp.net中的AOP框架 Microsoft.CCI, Mono.Cecil, Typemock Open-AOP API, PostSharp -摘自网络 (可以利用反射 Attribute 进行面向切面编程 可以用在记录整个方法的Log方面)
Both Microsoft.CCI and Mono.Cecil are low-level, and don't validate produced assemblies. It takes lo ...
- Go语言 字符串
在所有编程语言中都涉及到大量的字符串操作,可见熟悉对字符串的操作是何等重要. Go中的字符串和C#中的一样(java也是),字符串内容在初始化后不可修改. 需要注意的是在Go中字符串是有UTF-8编码 ...