题目1367:二叉搜索树的后序遍历序列-九度
来源:互联网 发布:一元云购源码干嘛用的 编辑:程序博客网 时间:2024/06/03 00:26
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
输入:
每个测试案例包括2行:
第一行为1个整数n(1<=n<=10000),表示数组的长度。
第二行包含n个整数,表示这个数组,数组中的数的范围是[0,100000000]。
输出:
对应每个测试案例,如果输入数组是某二叉搜索树的后序遍历的结果输出Yes,否则输出No。
样例输入:
7
5 7 6 9 11 10 8
4
7 4 6 5
样例输出:
Yes
No
推荐指数:※
来源:http://ac.jobdu.com/problem.php?pid=1367
二叉搜索树的左子树都比更小,右子数都比根大。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>using namespace std;bool rebuild_tree(int low,int high,long long *arr){if(low>high)return true;int i,index=high;long long tmp=arr[high];for(i=low;i<high;i++){if(arr[i]>tmp){index=i;break;}}for(i=index+1;i<high;i++){if(arr[i]<tmp)return false;}return (rebuild_tree(low,index-1,arr)==true&&rebuild_tree(index+1,high-1,arr)==true);}int main(){int n,i;while(scanf("%d",&n)!=EOF){long long *arr=new long long [n];for(i=0;i<n;i++)scanf("%lld",&arr[i]);if(rebuild_tree(0,n-1,arr)==false)printf("No\n");elseprintf("Yes\n");}return 0;}
- 题目1367:二叉搜索树的后序遍历序列-九度
- 九度oj 题目1367:二叉搜索树的后序遍历序列
- 九度 题目1367:二叉搜索树的后序遍历序列
- [九度oj]题目1367:二叉搜索树的后序遍历序列
- 九度OJ-题目1367:二叉搜索树的后序遍历序列
- 九度 题目1367:二叉搜索树的后序遍历序列
- 【剑指Offer面试编程题】题目1367:二叉搜索树的后序遍历序列--九度OJ
- 题目1367:二叉搜索树的后序遍历序列
- 题目1367:二叉搜索树的后序遍历序列
- 题目1367:二叉搜索树的后序遍历序列
- 题目1367:二叉搜索树的后序遍历序列
- 题目1367:二叉搜索树的后序遍历序列
- 题目10:二叉搜索树的后序遍历序列
- 题目24:二叉树搜索的后序遍历序列
- 算法题目-二叉搜索树的后序遍历序列
- 【剑指Offer面试题】 九度OJ1367:二叉搜索树的后序遍历序列
- 算法题目---二叉搜索村的后序遍历序列
- 1367:二叉搜索树的后序遍历序列 @jobdu
- 硬盘盘符丢失修复
- mpeg2,mpeg4,h264编码标准的异同
- JVM线程资源同步及交互机制
- Eclipse总是提示The user operation is waiting for background work to complete的解决方法(ubuntu环境下)
- python学习第一天----模块之import和reload
- 题目1367:二叉搜索树的后序遍历序列-九度
- As3.0中的位图(Bitmap/BitmapData)编程
- 目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结
- 【MongoDB for Java】Java操作MongoDB
- 提取 Microsoft.ReportViewer.ProcessingObjectModel.dll Microsoft.ReportViewer.DataVisualization.dll 等dl
- hdu-Play the Dice
- implicit declaration of function 'kzalloc' 解决方法
- 转子“知乎”--穷人如何成为富人
- 静夜随笔