-
Notifications
You must be signed in to change notification settings - Fork 0
/
98.验证二叉搜索树.java
55 lines (49 loc) · 1.43 KB
/
98.验证二叉搜索树.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
/*
* @lc app=leetcode.cn id=98 lang=java
*
* [98] 验证二叉搜索树
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* long val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(long val) { this.val = val; }
* TreeNode(long val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
// return valid(root, null, null);
long[] result = traverse(root);
return result[0] == 1;
}
private boolean valid(TreeNode root, TreeNode max, TreeNode min) {
if (root == null)
return true;
if (max != null && max.val <= root.val)
return false;
if (min != null && min.val >= root.val)
return false;
return valid(root.left, root, min) && valid(root.right, max, root);
}
private long[] traverse(TreeNode root) {
if (root == null)
return new long[] { 1, Long.MAX_VALUE, Long.MIN_VALUE };
long[] left = traverse(root.left);
long[] right = traverse(root.right);
if (left[0] == 0 || right[0] == 0)
return new long[] { 0 };
if (root.val > left[2] && root.val < right[1])
return new long[] { 1, Math.min(left[1], root.val), Math.max(right[2], root.val) };
return new long[] { 0 };
}
}
// @lc code=end