forked from ely-mitu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateBinarySearchTree.cpp
117 lines (100 loc) · 2.72 KB
/
validateBinarySearchTree.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Source : https://oj.leetcode.com/problems/validate-binary-search-tree/
// Author : Hao Chen
// Date : 2014-07-05
/**********************************************************************************
*
* Given a binary tree, determine if it is a valid binary search tree (BST).
*
* Assume a BST is defined as follows:
*
* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
bool isValidBST(TreeNode *root) {
//travel the tree by inner-order
vector<TreeNode*> stack;
TreeNode* node = root;
vector<int> v;
while (stack.size()>0 || node!=NULL) {
if (node!=NULL){
stack.push_back(node);
node = node->left;
}else{
node = stack.back();
stack.pop_back();
v.push_back(node->val);
node = node->right;
}
}
//check the vector wehther sorted or not
for(int i=0; v.size()>0 && i<v.size()-1; i++){
if (v[i] >= v[i+1]) {
return false;
}
}
return true;
}
TreeNode* createTree(int a[], int n)
{
if (n<=0) return NULL;
TreeNode **tree = new TreeNode*[n];
for(int i=0; i<n; i++) {
if (a[i]==0 ){
tree[i] = NULL;
continue;
}
tree[i] = new TreeNode(a[i]);
}
int pos=1;
for(int i=0; i<n && pos<n; i++) {
if (tree[i]){
tree[i]->left = tree[pos++];
if (pos<n){
tree[i]->right = tree[pos++];
}
}
}
return tree[0];
}
int main()
{
cout << isValidBST(NULL) << endl;
int a[]={1,1};
cout << isValidBST(createTree(a, sizeof(a)/sizeof(int))) << endl;
int b[]={4,2,6,1,7,5,7};
cout << isValidBST(createTree(b, sizeof(b)/sizeof(int))) << endl;
int c[]={4,2,6,1,3,5,7};
cout << isValidBST(createTree(c, sizeof(c)/sizeof(int))) << endl;
return 0;
}