05 August 2008
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
Example 1:
1
2
3
4
5
6
2
/ \
1 3
Input: [2,1,3]
Output: true
Example 2:
1
2
3
4
5
6
7
8
9
5
/ \
1 4
/ \
3 6
Input: [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
一棵BST定义为:
如果该二叉树的左子树不为空,则左子树上所有节点的值均小于它的根节点的值; 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;它的左右子树也为二叉搜索树。
递归前序遍历,每次限定传送参数的范围
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean isValidBST(TreeNode node, long min, long max) {
if (node == null) {
return true;
}
if (node.val <= min || node.val >= max) {
return false;
}
return isValidBST(node.left, min, node.val) && isValidBST(node.right, node.val, max);
}
}
递归中序遍历,仔细想想为什么中序遍历只需判断节点的值一次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
long pre = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
// 按照中序遍历,访问左子树到底
if (!isValidBST(root.left)) {
return false;
}
if (root.val <= pre) { // 递归中“归”的部分进行判断
return false;
}
pre = root.val;
// 访问右子树
return isValidBST(root.right);
}
}
Iterative
1) Queue的BFS
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
class Solution {
Queue<TreeNode> queue = new LinkedList<>();
Queue<Long> upperList = new LinkedList<>(), lowerList = new LinkedList<>();
public boolean isValidBST(TreeNode root) {
long lower = Long.MIN_VALUE, upper = Long.MAX_VALUE, val;
update(root, lower, upper);
while (!queue.isEmpty()) {
// 这个不用size分层了,只需把node往队列里面加就行
TreeNode node = queue.poll();
lower = lowerList.poll();
upper = upperList.poll();
if (node == null) {
continue;
}
val = node.val;
if (val <= lower) {
return false;
}
if (val >= upper) {
return false;
}
update(node.left, lower, val);
update(node.right, val, upper);
}
return true;
}
void update(TreeNode node, long lower, long upper) {
queue.offer(node);
lowerList.offer(lower);
upperList.offer(upper);
}
}
2) Stack迭代实现前序遍历
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
class Solution {
Stack<TreeNode> st = new Stack<>();
Stack<Long> upperList = new Stack<>(),
lowerList = new Stack<>();
public boolean isValidBST(TreeNode root) {
long lower = Long.MIN_VALUE, upper = Long.MAX_VALUE, val;
update(root, lower, upper);
while (!st.empty()) {
root = st.pop();
lower = lowerList.pop();
upper = upperList.pop();
if (root == null) continue;
val = (long)root.val;
if (val <= lower) return false;
if (val >= upper) return false;
update(root.right, val, upper);
update(root.left, lower, val);
}
return true;
}
void update(TreeNode node, long lower, long upper) {
st.push(node);
lowerList.push(lower);
upperList.push(upper);
}
}