出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数;

分析:

  • 最远距离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):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树的更多相关文章

  1. [二叉树建树]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 ...

  2. [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 ...

  3. PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一

    Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...

  4. LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树

    中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...

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

  6. Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

    题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...

  7. 【C++】根据二叉树的前序遍历和中序遍历重建二叉树并输出后续遍历

    /* 现在有一个问题,已知二叉树的前序遍历和中序遍历: PreOrder:GDAFEMHZ InOrder:ADEFGHMZ 我们如何还原这颗二叉树,并求出他的后序遍历 我们基于一个事实:中序遍历一定 ...

  8. 51nod 1832 先序遍历与后序遍历【二叉树+高精度】

    题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...

  9. [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 ...

随机推荐

  1. Ubuntu10.04安装与配置nfs服务器(转载)

    转自:http://blog.chinaunix.net/uid-25885064-id-3177969.html 1.安装nfs服务   $ sudo apt-get install nfs-ker ...

  2. E20170524-gg

    Awesome   adj. 可怕的; 令人敬畏的; 使人畏惧的; 极好的;

  3. bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛【Floyd】

    floyd传递关系,一个牛能确定排名的条件是能和所有牛确定关系 #include<iostream> #include<cstdio> using namespace std; ...

  4. (10)用css建立表单

    1.用css建立表单 本篇资料主要介绍使用css设置表单元素的方法. 表单是网页与用户交互所不可缺少的元素,表单是网页的访问者进行交互的接口,例如大家都常遇到的:网上注册.网上登录.网上交易.网上投票 ...

  5. SQL 初级教程学习(四)

    1. union,union all SELECT E_Name FROM Employees_ChinaUNIONSELECT E_Name FROM Employees_USA 默认地,UNION ...

  6. Qt对象模型之一:信号和槽

    一.信号和槽机制概述 信号槽是 Qt 框架引以为豪的机制之一.所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal).这种发出是没有目 ...

  7. [SRM613~] TaroCheckers

    一定要注意Topcoder的提交机制 Links: 原题地址 Vjudge Solution 这道题思维比较巧妙. 一看就基本知道是一个Dp题. 首先转换一下,用列而不是行来设第一维的状态,因为每列只 ...

  8. Hdu 5361 In Touch (dijkatrs+优先队列)

    题目链接: Hdu 5361  In Touch 题目描述: 有n个传送机排成一排,编号从1到n,每个传送机都可以把自己位置的东西传送到距离自己[l, r]距离的位置,并且花费c,问从1号传送机到其他 ...

  9. SIFT特征点检测与匹配

    SIFT的步骤如下: (1) 尺度空间极值检测(Scale-space Extrema Detection) 也就是在多尺度高斯差分(Difference of Gauss)空间中检测极值点(3x3x ...

  10. zoj 3649 lca与倍增dp

    参考:http://www.xuebuyuan.com/609502.html 先说题意: 给出一幅图,求最大生成树,并在这棵树上进行查询操作:给出两个结点编号x和y,求从x到y的路径上,由每个结点的 ...