leetcode解题: Validate Binary Search Tree (98)

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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.

解法1: DFS

本题的思路在题意中已经给出来了。要validate一个BST,要满足的是left 和right都是valid BST,同时root的值要大于left的最大值,要小于right的最小值。这里就提示我们在进行遍历的时候要记录每一个子树的最大值和最小值。
用一个辅助结构struct来记录max,min和是否是validate tree。 从左至右判断是否满足BST的条件。
C++

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct resultSet {
int maxVal;
int minVal;
bool isValid;
resultSet (int maxv, int minv, bool isv):maxVal(maxv),minVal(minv),isValid(isv){};
};
class Solution {
public:
bool isValidBST(TreeNode* root) {
if (root == NULL) {
return true;
}
resultSet temp = helper(root);
return temp.isValid;
}
resultSet helper(TreeNode* root) {
if (!root->left && !root->right) {
return resultSet(root->val, root->val, true);
}
int maxVal = root->val;
int minVal = root->val;
if (root->left) {
resultSet left = helper(root->left);
if (!left.isValid || root->val <= left.maxVal) {
return resultSet(0, 0, false);
}
maxVal = max(maxVal, left.maxVal);
minVal = min(minVal, left.minVal);
}
if (root->right) {
resultSet right = helper(root->right);
if (!right.isValid || root->val >= right.minVal) {
return resultSet(0, 0, false);
}
maxVal = max(maxVal, right.maxVal);
minVal = min(minVal, right.minVal);
}
return resultSet(maxVal, minVal, true);
}
};

Java

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class Node {
int min;
int max;
boolean isValid;
public Node(int min, int max, boolean isValid) {
this.min = min;
this.max = max;
this.isValid = isValid;
}
};
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
Node res = helper(root);
return res.isValid;
}
private Node helper(TreeNode root) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return new Node(root.val, root.val, true);
}
Node left = helper(root.left);
Node right = helper(root.right);
// conquer
if (left == null) {
if (!right.isValid || right.min <= root.val) {
return new Node(-1, -1, false);
} else {
return new Node(root.val, right.max, true);
}
} else if (right == null) {
if (!left.isValid || left.max >= root.val) {
return new Node(-1, -1, false);
} else {
return new Node(left.min, root.val, true);
}
} else {
if (!left.isValid || !right.isValid) return new Node(-1, -1, false);
if (left.max >= root.val || right.min <= root.val) return new Node(-1, -1, false);
return new Node(Math.min(left.min, root.val), Math.max(right.max, root.val), true);
}
}
}