-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-98-Validate-Binary-Search-Tree.java
More file actions
93 lines (75 loc) · 3.27 KB
/
LeetCode-98-Validate-Binary-Search-Tree.java
File metadata and controls
93 lines (75 loc) · 3.27 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
LeetCode:
LintCode: http://www.lintcode.com/problem/validate-binary-search-tree/
JiuZhang: http://www.jiuzhang.com/solutions/validate-binary-search-tree/
ProgramCreek: http://www.programcreek.com/2012/12/leetcode-validate-binary-search-tree-java/
Other: http://www.cnblogs.com/yuzhangcmu/p/4177047.html
Analysis: 4 solutions
1.Recursive
2.Iterative
Do inorder Traversal, and compare the order is ascending.
To compare the order is ascending, we need another TreeNode variable to record the previous node, and compare the prev and curr.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// 1. DFS
// public boolean isValidBST(TreeNode root) {
// Double.MIN_VALUE: A constant holding the smallest positive nonzero value of type double, 2-1074.
// Double.MAX_VALUE: A constant holding the largest positive finite value of type double, (2-2-52)·21023.
// So here we cannot use Double.MIN_VALUE and Double.MAX_VALUE
// return isValidBST(root, Double.MIN_VALUE, Double.MAX_VALUE);
// return isValidBST(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
// }
// private boolean isValidBST(TreeNode root, double min, double max) {
// if (root == null) return true;
// if (root.val >= max || root.val <= min) return false;
// return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max);
// }
// 2.DFS Iteration (using Inorder Traversal)
// public boolean isValidBST(TreeNode root) {
// if (root == null) return true;
// Stack<TreeNode> stack = new Stack<>();
// TreeNode curr = root;
// TreeNode prev = null; // record the prev node (which is left child of curr), and check if the prev.val < curr.val
// while (curr != null || !stack.isEmpty()) {
// // Put everything in the left tree into the stack.
// while (curr != null) {
// stack.push(curr);
// curr = curr.left;
// }
// curr = stack.pop();
// // Prev is the left node, which should prev.val < curr.val
// if (prev != null && prev.val >= curr.val) return false;
// prev = curr;
// curr = curr.right;
// }
// return true;
// }
// 3. BFS (Using another way of Inorder Traversal, easier to understand the approach)
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
TreeNode prev = null; // record the prev node (which is left child of curr), and check if prev.val < curr.val
while (curr != null || !stack.isEmpty()) {
if (curr != null) {
stack.push(curr);
curr = curr.left;
} else {
curr = stack.pop();
if (prev != null && prev.val >= curr.val) return false;
prev = curr;
curr = curr.right;
}
}
return true;
}
}