-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree_98_Validata_BST.cpp
executable file
·61 lines (50 loc) · 1.71 KB
/
Tree_98_Validata_BST.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
/*
此题极度类似判断树是否平衡, 自顶向下的算法包含了两个重复的计算;
希望通过子底向上的算法合并之。
类似题目: 110
*/
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
//bool isValidBST_BruteSolve(TreeNode* root) {
// isValidBST(root->left) && isValidBST(root-right) && root->val > MaxNode(root->left) && root->val < MaxNode(root->right);
//}
bool isValidBST_helper(TreeNode* root, TreeNode* maxNode, TreeNode* minNode){
if(!root) return true;
if((maxNode && root->val > maxNode->val) || (minNode && root->val <= minNode->val))
return false;
return isValidBST_helper(root->left, root, minNode) && isValidBST_helper(root->right, maxNode, root);
}
bool isValidBST(TreeNode* root){
return isValidBST_helper(root, NULL, NULL);
}
};
int main(){
TreeNode* EmptyTree = NULL;
TreeNode* OneNodeTree = new TreeNode(1);
TreeNode* LeftTree = new TreeNode(1);
LeftTree->left = new TreeNode(2);
LeftTree->left->left = new TreeNode(3);
TreeNode* BigTree = new TreeNode(1);
BigTree->left = new TreeNode(2);
BigTree->left->left = new TreeNode(3);
BigTree->left->right = new TreeNode(4);
BigTree->right = new TreeNode(5);
BigTree->right->right = new TreeNode(6);
// 1
// 2 5
// 3 4 6
Solution Sol = Solution();
cout<<"Emptytree: "<<Sol.isValidBST(EmptyTree)<<endl;
cout<<"OneNodeTree: " <<Sol.isValidBST(OneNodeTree)<<endl;
cout<<"LeftTree: " <<Sol.isValidBST(LeftTree)<<endl;
cout<<"BigTree: " <<Sol.isValidBST(BigTree)<<endl;
//cout<<Sol.traverse(BigTree);
}