《Cracking the Coding Interview》——第4章:树和图——题目6
2014-03-19 04:16
题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点。
解法1:分两种情况:向下走时,先右后左;向上走时,先左后右。如果目标节点有右子树,就向右下走,否则往左上走。话说,如果没有父指针的话,还是一口气进行各中序遍历,求出所有结果比较有效率。
代码:
// 4.6 Find the inorder successor of a node in the binary tree.
// online algorithm with parent pointer.
#include <cstdio>
#include <unordered_map>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode *parent; TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr), parent(nullptr) {};
}; void constructBinaryTree(TreeNode *&root)
{
int val; scanf("%d", &val);
if (val <= ) {
root = nullptr;
} else {
root = new TreeNode(val); constructBinaryTree(root->left);
constructBinaryTree(root->right);
if (root->left != nullptr) {
root->left->parent = root;
}
if (root->right != nullptr) {
root->right->parent = root;
}
}
} TreeNode *inorderSuccessor(TreeNode *node)
{
if (node == nullptr) {
return nullptr;
} TreeNode *result;
if (node->right != nullptr) {
result = node->right;
while (result->left != nullptr) {
result = result->left;
} return result;
} else {
result = node;
while(true) {
if (result->parent == nullptr) {
return nullptr;
} else if (result == result->parent->left) {
return result->parent;
} else {
result = result->parent;
}
}
}
} void inorderTraversal(TreeNode *root)
{
if (root == nullptr) {
return;
} inorderTraversal(root->left);
TreeNode *next_node = inorderSuccessor(root);
if (next_node != nullptr) {
printf("%d %d\n", root->val, next_node->val);
} else {
printf("%d #\n", root->val);
}
inorderTraversal(root->right);
} void clearBinaryTree(TreeNode *&root) {
if (root == nullptr) {
return;
} else {
clearBinaryTree(root->left);
clearBinaryTree(root->right);
delete root;
root = nullptr;
}
} int main()
{
TreeNode *root; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
} inorderTraversal(root); clearBinaryTree(root);
} return ;
}
解法2:一次性进行中序遍历的离线做法。
代码:
// 4.6 Find the inorder successor of a node in the binary tree.
// offline algorithm with inorder traversal.
#include <cstdio>
#include <unordered_map>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode *parent; TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr), parent(nullptr) {};
}; void constructBinaryTree(TreeNode *&root)
{
int val; scanf("%d", &val);
if (val <= ) {
root = nullptr;
} else {
root = new TreeNode(val); constructBinaryTree(root->left);
constructBinaryTree(root->right);
if (root->left != nullptr) {
root->left->parent = root;
}
if (root->right != nullptr) {
root->right->parent = root;
}
}
} void inorderTraversal(TreeNode *root, vector<TreeNode *> &inorder_list)
{
if (root == nullptr) {
return;
}
inorderTraversal(root->left, inorder_list);
inorder_list.push_back(root);
inorderTraversal(root->right, inorder_list);
} TreeNode *inorderSuccessor(TreeNode *node, unordered_map<TreeNode *, TreeNode *> &dict)
{
if (dict.find(node) == dict.end()) {
return nullptr;
} else {
return dict[node];
}
} void clearBinaryTree(TreeNode *&root) {
if (root == nullptr) {
return;
} else {
clearBinaryTree(root->left);
clearBinaryTree(root->right);
delete root;
root = nullptr;
}
} int main()
{
TreeNode *root;
unordered_map<TreeNode *, TreeNode *> dict;
vector<TreeNode *> inorder_list;
int i; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
} inorderTraversal(root, inorder_list);
for (i = ; i < (int)inorder_list.size() - ; ++i) {
dict[inorder_list[i]] = inorder_list[i + ];
}
dict[inorder_list[i]] = nullptr; TreeNode *next_node;
for (i = ; i < (int)inorder_list.size(); ++i) {
next_node = inorderSuccessor(inorder_list[i], dict);
if (next_node != nullptr) {
printf("%d %d\n", inorder_list[i]->val, next_node->val);
} else {
printf("%d #\n", inorder_list[i]->val);
}
} inorder_list.clear();
dict.clear();
clearBinaryTree(root);
} return ;
}
《Cracking the Coding Interview》——第4章:树和图——题目6的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 《Cracking the Coding Interview》——第4章:树和图——题目9
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
- 《Cracking the Coding Interview》——第4章:树和图——题目8
2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...
随机推荐
- 使用navigate导出表数据
以下内容不算技术贴,只能算是技巧贴,要做的一个操作,从数据库A中把元素A1表,导入到数据库B中B1表,且,A1表的数据是部分导出,那么有两种方法进行导出 方法1: 选择数据表,右键,选择“转存储sql ...
- url网址解析的好帮手
接下来进入node的重点,也就是介绍nodejs主要api的功能和如果使用,由于nodejs逐渐能满足这种高并发和大规模的场景.他才被更多的公司所采用 无论什么资源,一定要有明确的地址才有意义,在互联 ...
- redis 系列 在 vs上 set,get 键值
1.启动两个 cmd,一个用于打开服务,一个用于运行客户端. 详细步骤可见上一篇文章 2.下载nuget的 ServiceStack.Redis; ,并在using中引用 ,详细步骤可见上一篇文章 ...
- maven没有servlet(创建servlet后报错)
maven不能创建servlet 解决方案 方案一 在项目的iml进行指定根目录 <sourceRoots> <root url="file://$MODULE_DIR$/ ...
- mysql题目(二学年)
1.哪些命令可以知道mysql安装的版本 mysqladmin --version mysql --version 2.关于mysql密码说法正确的是 初始化安装完毕后密码为空 3.进入或者打开数据库 ...
- 第30章 ADC—电压采集—零死角玩转STM32-F429系列
第30章 ADC—电压采集 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...
- 第18章 SysTick—系统定时器—零死角玩转STM32-F429系列
第18章 SysTick—系统定时器 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...
- DateTools,可能是最好用的iOS日期工具库
项目简介 DateTools 用于提高Objective-C中日期和时间相关操作的效率.灵感来源于 DateTime和Time Period Library. 项目主页: DateTools 最新示例 ...
- 最大公约数(gcd模板)
int gcd(int a,int b) { ) { int t=a%b; a=b; b=t; } return a; }
- Python线程创建与使用
Python多为线程编程提供了两个简单明了的模块:thread和threading,Python3中已经不存thread模块,已经被改名为_thread,实际优先使用 threading模块. 1.P ...