-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidateBinarySearchTree.java
More file actions
85 lines (69 loc) · 2.54 KB
/
Copy pathValidateBinarySearchTree.java
File metadata and controls
85 lines (69 loc) · 2.54 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
// Given a binary tree, determine if it is a valid binary search tree (BST).
// See: https://leetcode.com/problems/validate-binary-search-tree/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class ValidateBinarySearchTree {
/**
* Solution 1
*
* Not optimal solution with O(N) time complexity and O(N) space.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
public boolean isValidBST_slow(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
dfs(root, res);
for (int i = 1; i < res.size(); i++) {
if (res.get(i) <= res.get(i-1)) {
return false;
}
}
return true;
}
private void dfs(TreeNode root, List<Integer> res) {
if (root != null) {
dfs(root.left, res);
res.add(root.val);
dfs(root.right, res);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Solution 2
*
* Optimized solution with O(N) time complexity and O(1) space.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
private boolean isBinarySearchTree = true;
private Integer prev = null;
public boolean isValidBST(TreeNode root) {
dfsOpt(root);
return isBinarySearchTree;
}
private void dfsOpt(TreeNode root) {
if (root != null && isBinarySearchTree) {
dfsOpt(root.left);
if (prev != null && prev >= root.val) {
isBinarySearchTree = false;
return;
}
prev = root.val;
dfsOpt(root.right);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// TODO: Solution 3: Recursion solution
// TODO: Solution 4: DFS with stack solution
public static void main(String[] args) {
TreeNode n1 = initTree(5,1,4,null,null,3,6);
System.out.println(new ValidateBinarySearchTree().isValidBST(n1));
TreeNode n2 = initTree(2, 1, 3);
System.out.println(new ValidateBinarySearchTree().isValidBST(n2));
TreeNode n3 = initTree(10, 5, 15 ,null, null, 6, 20);
System.out.println(new ValidateBinarySearchTree().isValidBST(n3));
TreeNode n4 = initTree(1, 1);
System.out.println(new ValidateBinarySearchTree().isValidBST(n4));
}
}