《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一模一样. 解法:首先可以根据 ...
随机推荐
- TP5.1:依赖注入、绑定一个类到容器里、绑定一个闭包到容器中
依赖注入 1.在application中创建一个文件夹,名字为commom,commom文件夹中创建被注入文件夹,在被注入文件夹中创建一个名为demo.php的文件 2.在demo.php中输入: 3 ...
- 大数据量高并发的数据库优化详解(MSSQL)
转载自:http://www.jb51.net/article/71041.htm 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能. ...
- 前端js限制上传文件类型及大小(1)
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 了解iOS上的可执行文件和Mach-O格式
http://www.cocoachina.com/mac/20150122/10988.html http://www.reinterpretcast.com/hello-world-mach-o ...
- SSM框架整合:转自:http://blog.csdn.net/zhshulin
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- when 让你跳出异步回调噩梦 node.js下promise/A规范的使用
其实关于promise 的博客,前端时间专门写了一篇关于 promise 规范的文章,promise规范 让 javascript 中的异步调用更加人性化. 简单回忆下: promise/A规范定义的 ...
- 第25章 串行FLASH文件系统FatFs—零死角玩转STM32-F429系列
第25章 串行FLASH文件系统FatFs 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.c ...
- JS无限添加HTML到指定位置
用JS把HTML添加到指定位置有两种写法,一种是用字符串,一种是用javascript中的方法 第一种: 用字符串写 <h2>利用JS无限添加一个相同部分</h2> <h ...
- MySQL常见错误分析与解决方法总结
MySQL常见错误分析与解决方法总结 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分 ...
- Java之 jstl 自定义标签的方法
1.写一个Java类 我的路径是写再tag包中的一个 HelloTag类 package tag; import java.io.IOException; import javax.servlet.j ...