-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree1.cpp
More file actions
37 lines (28 loc) · 764 Bytes
/
Tree1.cpp
File metadata and controls
37 lines (28 loc) · 764 Bytes
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
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
bool inorder(TreeNode* root, long long &prev) {
if (!root) return true;
if (!inorder(root->left, prev)) return false;
if (root->val <= prev) return false; // not strictly increasing
prev = root->val;
return inorder(root->right, prev);
}
bool isValidBST(TreeNode* root) {
long long prev = -1e18; // very small value
return inorder(root, prev);
}
int main() {
TreeNode* root = new TreeNode(2);
root->left = new TreeNode(1);
root->right = new TreeNode(3);
if (isValidBST(root))
cout << "true";
else
cout << "false";
return 0;
}