-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo98ValidateBinarySearchTree.java
More file actions
38 lines (32 loc) · 1.16 KB
/
No98ValidateBinarySearchTree.java
File metadata and controls
38 lines (32 loc) · 1.16 KB
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
package com.wzx.leetcode;
import com.wzx.entity.TreeNode;
/**
* @see <a href="https://leetcode.com/problems/validate-binary-search-tree/">https://leetcode.com/problems/validate-binary-search-tree/</a>
* @author wzx
*/
public class No98ValidateBinarySearchTree {
/**
* 递归
* <p>
* time: O(n)
* space: O(n)
*/
public boolean isValidBST(TreeNode root) {
// null标记初始值, 不能使用Integer.MAX_VALUE和Integer.MIN_VALUE作为初始值
return recursion(root, null, null);
}
/**
* root为当前子树根结点, 取值范围在(min, max)
* @param max: 右子树的最小值
* @param min: 左子树的最大值
*/
private boolean recursion(TreeNode root, Integer max, Integer min) {
if (root == null) return true;
if (max != null && root.val >= max) return false;
if (min != null && root.val <= min) return false;
// 左子树的所有结点小于根结点值, 所以根结点值为左子树的最大值
return recursion(root.left, root.val, min)
// 右子树的所有结点大于根结点值, 所以根结点值为右子树的最小值
&& recursion(root.right, max, root.val);
}
}