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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. 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 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

  10. 《Cracking the Coding Interview》——第4章:树和图——题目8

    2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...

随机推荐

  1. 141. Linked List Cycle (amazon)

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  2. Gym 100090M Jumping along the Hummocks

    题意: 从 前往后跳,要么跳一步,跳到相邻的位置,要么跳到下一个数字相同的位置,求跳到最后的最少步数. dp,但是会tle,我用map优化了一下. #include <bits/stdc++.h ...

  3. 简单介绍Spring是什么?

    对于面试者回答什么是Spring,这个问题占6分分值,分值点分布:1.Spring的核心是一个轻量级(Lightweight)的容器(Container).2.Spring是实现IoC(Inversi ...

  4. office2010激活

    软件下载链接: http://yunpan.cn/cySGrE99u6uv3 (提取码:c612) 下面是操作演示,我录制成gif文件了,下载下来用浏览器打开 360网盘:http://yunpan. ...

  5. 正定矩阵(Positive-definite Matrix)

    原文链接 正定矩阵是自共轭矩阵的一种.正定矩阵类似复数中的正实数.定义:对于对称矩阵M,当且仅当存在任意向量x,都有 若上式大于等于零,则称M为半正定矩阵.正定矩阵记为M>0.也被称为正定二次型 ...

  6. CSS font-size字体大小样式属性

    设置字体大小CSS单词与语法 基本语法结构: .divcss5{font-size:12px;}设置了文字大小为12px像素Font-size+字体大小数值+单位 单词:font-size语法:fon ...

  7. 旧文备份:对象字典0x1005和0x1006的理解

    SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...

  8. css权值问题

    继承是没有权值的,比通配符的的权值0还要低. 选择器是不分上下级的.只管优先级. 第一等:代表内联样式,如: style=””,权值为1000. 第二等:代表ID选择器,如:#content,权值为0 ...

  9. 如何使用MongoDB+Springboot实现分布式ID?

    转载请标明出处: http://blog.csdn.net/forezp/article/details/69056017 本文出自方志朋的博客 一.背景 如何实现分布式id,搜索相关的资料,一般会给 ...

  10. 顺序语句:GOTO和NULL语句

    一 标号和GOTO 1 语法: PL/SQL中GOTO语句是无条件跳转到指定的标号去的意思.语法如下: GOTO label;......<<label>> /*标号是用< ...