码迷,mamicode.com
首页 > 其他好文 > 详细

[Jobdu]题目1367:二叉搜索树的后序遍历序列

时间:2015-08-04 20:41:51      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <cstdio>
 2  
 3 int isValid(int a[], int low, int high) {
 4     if (low >= high)
 5         return 1;
 6  
 7     int root = a[high];
 8     int i = low;
 9     while (a[i] < root && i <= high)
10         i++;
11     for (int j = i; j < high; j++)
12         if (a[j] < root)
13             return false;
14     return isValid(a, low, i - 1) && isValid(a, i, high - 1);
15 }
16  
17 int main() {
18     int n;
19     while (scanf("%d", &n) != EOF) {
20         int a[n];
21         for (int i = 0; i < n; ++i)
22             scanf("%d", &a[i]);
23  
24         if (isValid(a, 0, n - 1)) {
25             printf("Yes\n");
26         } else {
27             printf("No\n");     
28         }
29     }
30     return 0;
31 }
32 /**************************************************************
33     Problem: 1367
34     User: tonyhu
35     Language: C++
36     Result: Accepted
37     Time:10 ms
38     Memory:1020 kb
39 ****************************************************************/

 

[Jobdu]题目1367:二叉搜索树的后序遍历序列

标签:

原文地址:http://www.cnblogs.com/tonyhu1993/p/4702979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!