-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
38 lines (33 loc) · 686 Bytes
/
BST.cpp
File metadata and controls
38 lines (33 loc) · 686 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
38
#include <stdio.h>
using namespace std;
class TestTree
{
public:
TestTree(int v) : val(v), left(nullptr), right(nullptr) {};
int val;
TestTree* left;
TestTree* right;
};
bool isValidTree(TestTree* tr, int lover, int upper)
{
if (!tr)
return true;
int val = tr->val;
if (val <= lover || val >= upper)
return false;
if (!isValidTree(tr->right, val, upper))
return false;
if (!isValidTree(tr->left, lover, val))
return false;
return true;
}
int main()
{
TestTree testtree(5);
testtree.left = new TestTree(7);
testtree.right = new TestTree(4);
std::cout << isValidTree(&testtree,
std::numeric_limits::min(),
std::numeric_limits::max());
return 0;
}