forked from gatieme/CodingInterviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathispost_n2.cpp
78 lines (62 loc) · 1.5 KB
/
ispost_n2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
using namespace std;
// 调试开关
#define __tmain main
#ifdef __tmain
#define debug cout
#else
#define debug 0 && cout
#endif // __tmain
class Solution
{
public:
bool VerifySquenceOfBST(vector<int> sequence)
{
if(sequence.size( ) == 0)
{
return false;
}
int count = 0;
int left = 0, right = sequence.size( ) - 1;
while(right > 0)
{
// 前一半的元素都小于size
while(sequence[left] < sequence[right])
{
left++;
count++;
}
// 循环结束时, left是第一个大于根的元素的位置
// 其后面的元素都应该比根大
while(sequence[left] > sequence[right])
{
left++;
count++;
}
// 正常情况下, 循环结束时, left应该等于right
debug <<left <<" " <<right <<endl;
if(left < right)
{
return false;
}
right--;
left = 0;
}
cout <<count <<endl;
// 如果循环一直走到right == 0才终止, 说明满足后序遍历的定义
return true;
}
};
int __tmain( )
{
// 10
// 6 14
// 8 12 16
//4
int a[] = { 2, 9, 5, 16, 17, 15, 19, 18, 12, };
vector<int> vec(a, a + 9);
Solution solu;
cout <<solu.VerifySquenceOfBST(vec) <<endl;
return 0;
}