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

分析:

  • 最远距离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. 基于《Hadoop权威指南 第三版》在Windows搭建Hadoop环境及运行第一个例子

    在Windows环境上搭建Hadoop环境需要安装jdk1.7或以上版本.有了jdk之后,就可以进行Hadoop的搭建. 首先下载所需要的包: 1. Hadoop包: hadoop-2.5.2.tar ...

  2. EF 连接MySql

    使用EntityFramework6连接MySql数据库(db first方式) http://www.cnblogs.com/24la/archive/2014/04/03/ef6-mysql.ht ...

  3. javascript判断页面是否在移动设备上打开

    var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OS\s([\d_]+)/), isIphone =!ipad && ...

  4. bzoj 1009: [HNOI2008]GT考试【kmp+dp+矩阵快速幂】

    看n和k的范围长得就很像矩阵乘法了 设f[i][j]表示到第i个位置的后缀最长匹配目标串的j位.转移的话显然是枚举0~9,然后选择f[i+1]中能被他转移的加起来,需要用到next数组.然后构造矩阵的 ...

  5. mysql查询流程

    首先是连接器 连接器负责跟客户端来链接 链接成功后 mysql会先去查询缓存,之前是不是有查询的这条语句,之前执行过的话 就会以key-value的形式缓存到内存中,如果没有就会继续执行后面的,执行完 ...

  6. iOS 切割圆角图片、图片文件格式判断

    1.切割圆角图片 // 性能不好,适合圆角图形数量比较少的情况 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMak ...

  7. Hdu 3294 Girls' research (manacher 最长回文串)

    题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...

  8. iOS 实现复选框 checkbox --转

    转自:http://www.cnblogs.com/ygm900/p/3468891.html  -(void)checkboxClick:(UIButton *)btn{    btn.select ...

  9. 函数的返回值return

    '''1.什么是返回值 返回值是一个函数的处理结果 2.为什么要有返回值 如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值 3.函数的返回值的应用 函数的返回值用retur ...

  10. Linux 之 2>&1

    我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...