笔试算法题(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 ...
随机推荐
- 2-6 Opencv模块组织结构
https://opencv.org/releases.html https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.4 ...
- 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】
首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...
- Luogu P1004/P1006 方格取数/传纸条 【棋盘Dp】 By cellur925
我明明记得写过这篇啊qwq为什么会搞丢 两题几乎一样. 如果再拓展到k条路,就要用网络流跑了,本蒟现在还不会. 我们容易想到四维dp,但是有一种更好的方法. 首先,先从左上到右下.再从右下到左上可以近 ...
- centos 7添加快捷键
转自:http://www.cnblogs.com/flying607/p/5730867.html centos7中不自带启动终端的快捷键,可以自定义添加. 点击右上角的用户名,点击设置,在设置面板 ...
- 第四章之S5PV210内存初始化
1,既然UART可以打印出信息来,那我们可以打印内存中的值.在506行添加如下代码: /***UART transmit function by xu ***/ display_addr_dat: l ...
- 例题 3-5 生成元 digit generator
#include<stdio.h> #include<string.h> #define maxn 100005 int ans[maxn]; //类似于 比较大的数组还是开导 ...
- [POI2009]Kon
Description 火车沿途有N个车站,告诉你从每一站到每一站的人数,现在查票员只能查K次票,每次查票可以控制目前在车上的所有乘客的车票.求一个查票方案,使得控制的不同的乘客尽量多. (显然对同一 ...
- [Usaco2006 Jan] Dollar Dayz 奶牛商店
Description 约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,-,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢? Inpu ...
- _bzoj1003 [ZJOI2006]物流运输【预处理】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1003 预处理出第i天到第j天走一条航线时的最短路. #include <cstdio& ...
- python面向对象的3个特点
封装 封装是从业务逻辑中抽象对象时,要赋予对象相关数据与操作,将一些数据和操作打包在一起的过程.封装是使用对象的主要魅力之一,它提供了一个简单方法来创建复杂方案,解决了世界是如何工作的这一问题,我们自 ...