-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0098.java
More file actions
45 lines (39 loc) · 1.21 KB
/
Copy pathLeetCode0098.java
File metadata and controls
45 lines (39 loc) · 1.21 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
39
40
41
42
43
44
45
/* Validate Binary Search Tree
* Input:
* 5
/ \
1 4
/ \
3 6
* Output: false
* */
import java.util.Stack;
public class LeetCode0098 {
public static int index = 0;
public static int[] TREE_VALUE = new int[]{5, 1, 0, 0, 4, 3, 0, 0, 6, 0, 0};
public static void main(String args[]) {
TreeNode root = new TreeNode();
root = TreeNode.createTree(root, index, TREE_VALUE);
System.out.println(isValidBST(root));
}
//二分搜索树的中序遍历为从小到大排列
public static boolean isValidBST(TreeNode root) {
if (root == null)
return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
double last = - Double.MAX_VALUE; //题目有一个[-2147483648]测试例
while (curr != null || !stack.isEmpty()){
while (curr != null){
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
if (curr.val <= last)
return false;
last = curr.val;
curr = curr.right;
}
return true;
}
}