笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数;
分析:
- 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点;所以计算出以每个节点为根节点的子树的最 远距离,最后取他们的最大值就是整棵树的最远距离;
- 如果递归层次过多造成系统栈溢出,则可以使用stack堆栈结构存储递归节点,从而使用循环实现
解题:
struct Node {
int value;
Node *left;
Node *right;
int leftDis;
int rightDis;
}; void MaxDistance(Node *root, int *MaxDis) {
/**
* 三个递归停止条件
* */
if(root==NULL)
return;
if(root->left==NULL)
root->leftDis=;
if(root->right==NULL)
root->rightDis=; /**
* 处理当前节点之前,首先处理子节点
* */
if(root->left!=NULL)
MaxDistance(root->left, MaxDis);
if(root->right!=NULL)
MaxDistance(root->right, MaxDis); /**
* 递归仅处理了root->left和root->right为根节点的
* 最远距离,所以当前已经知道root->left和root->right
* 各自的leftDis和rightDis
* */
if(root->left!=NULL) {
int tempMax=;
if(root->left->leftDis > root->left->rightDis)
tempMax=root->left->leftDis;
else
tempMax=root->left->rightDis;
root->leftDis=tempMax+;
}
if(root->right!=NULL) {
int tempMax=;
if(root->right->leftDis > root->right->rightDis)
tempMax=root->right->leftDis;
else
tempMax=root->right->rightDis;
root->rightDis=tempMax+;
}
/**
* 更新全局的最远距离MaxDis,最初调用的时候需要赋值为-1
* */
if(*MaxDis < root->leftDis + root->rightDis)
*MaxDis=root->leftDis + root->rightDis;
}
出题:如果已经知道一棵二叉树的前序和中序遍历结果,如何快速重建二叉树。如果知道前序和后序,或者知道中序和后序,是否仍旧可以重建;
分析:
- 下述为一个二叉树的例子,对应的前序,中序和后序遍历如下:
Pre: abdehcfgi
In: dbehafcig
Suffix: dhebfigca
![]() |
- 如果仅知道Pre和In,Pre序列的第一个字符必定为当前子树的根节点(a),所以对应到In序列中,可以根节点为分界(a)将左右子树分开(dbeh 和fcig),然后递归直到仅剩下一个字符;
- Suffic和In也同理,但是仅知道Pre和Suffix不能重建二叉树。
解题:
struct Node {
int value;
Node *left;
Node *right;
}; Node* RestoreTree(char *pre, int pre1, int pre2,
char *in, int in1, int in2) {
if(pre1>pre2 || in1>in2) return NULL;
/**
* 当前pre的第一个字符必定是一棵子树的根节点
* 所以首先创建一个节点
* */
Node *temp=new Node();
temp->value=pre[pre1];
temp->left=NULL;
temp->right=NULL; /**
* 当pre1和pre2相等时,说明已经只有一个字符
* 则说明二叉树已经到达子节点,直接返回
* */ if(pre1==pre2 || in1==in2)
return temp; /**
* 查找pre[pre1]在in序列中的位置
* */
int i;
for(i=in1;i<=in2;i++) {
if(pre[pre1]==in[i])
break;
} /**
* 对pre和in序列进行划分,注意当一个节点仅有左子节点
* 或者只有右子节点时,pre1可能大于pre2
* */
temp->left=RestoreTree(pre, pre1+, pre1+(i-in1),
in, in1, i-);
temp->right=RestoreTree(pre, pre1+(i-in1)+, pre2,
in, i+, in2); return temp;
} void showTree(Node *root) {
if(root==NULL)
return; if(root->left!=NULL && root->right!=NULL) {
printf("%c, %c\n",root->left->value,
root->right->value);
showTree(root->left);
showTree(root->right);
} else if(root->left!=NULL) {
printf("%c\n",root->left->value);
showTree(root->left);
} else if(root->right!=NULL) {
printf("%c\n",root->right->value);
showTree(root->right);
}
} int main() {
char pre[]="abdehcfgi";
char in[]="dbehafcig"; Node *root=RestoreTree(pre, , , in, , );
showTree(root);
return ;
}
笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树的更多相关文章
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...
- 【C++】根据二叉树的前序遍历和中序遍历重建二叉树并输出后续遍历
/* 现在有一个问题,已知二叉树的前序遍历和中序遍历: PreOrder:GDAFEMHZ InOrder:ADEFGHMZ 我们如何还原这颗二叉树,并求出他的后序遍历 我们基于一个事实:中序遍历一定 ...
- 51nod 1832 先序遍历与后序遍历【二叉树+高精度】
题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
随机推荐
- final的好处
1.final关键字提高了性能.JVM和Java应用都会缓存final变量. 2.final变量可以安全的在多线程下进行共享,而不需要额外的同步开销. 3.使用final关键字,JVM会对方法,变量和 ...
- android:layout_gravity 和 android:gravity 的区别(转载)
转自:http://www.cnblogs.com/ghj1976/archive/2011/04/26/2029535.html gravity 这个英文单词是重心的意思,在这里就表示停靠位置的意思 ...
- bzoj 1306: [CQOI2009]match循环赛【dfs+剪枝】
大力剪枝,最后洛谷上还开了o2才过-- 大概这样剪枝: 1.搜索中,一个队当前得分超过要求或者一个队剩下的比赛场数全赢也达不到要求则return: 2.注意到如果平局,最总分的贡献是2,否则是3,所以 ...
- PowerShell+NetApi 批处理执行程序
基于CMD 编写批处理程序很反人类,此篇文章介绍利用Power Shell脚本(借鉴明经net版主雪山飞狐的脚本改造而成)处理之前的Bat+scr的过程(https://www.cnblogs.com ...
- VS2019 字符串对指针char*赋值编译器报错原因及解决方法
2019-05-26 21:55:08 前几天在敲代码时,将字符串“Hellow world!”赋值给指针char*类型指针时编译器报错的问题 网上搜索后发现 char*是历史遗留问题,如果程序修 ...
- 在JS/jQuery中,怎么触发input的keypress/keydown/keyup事件?
怎么触发keypress/keydown/keyup事件? 问题: 1.在之前的写的input后面添加了搜索按钮 2.input只有keyup事件,如下: $("#desktop_folde ...
- 《windows核心编程系列》十五谈谈windows线程栈
谈谈windows线程栈. 当系统创建线程时会为线程预订一块地址空间区域,注意仅仅是预订.默认情况下预定的这块区域的大小是1MB,虽然预订这么多,但是系统并不会给全部区域调拨物理存储器.默认情况下,仅 ...
- jQuery 动作效果
隐藏和显示 jQuery hide() 和 show() 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: jQuery toggle() 通过 jQ ...
- DFS和BFS模板
DFS: 该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想 #include<cstdio> #include<cstring> #include<cstdli ...
- 01背包(分组) HDOJ 4341 Gold miner
题目传送门 题意:有n个金矿,每个金矿有抓取的消耗的时间和价值,矿工在原点,问在T时间内能得到的最大的价值 分析:唯一和01背包不同的是金矿可能共线,也就是抓取近的金矿后才能抓后面共线的金矿.这是分组 ...