-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00098-validate_binary_search_tree.java
99 lines (80 loc) · 2.84 KB
/
00098-validate_binary_search_tree.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
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
94
95
96
97
98
99
// 98: Validate Binary Search Tree
// https://leetcode.com/problems/validate-binary-search-tree/
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
class Solution {
public static class TreeNode {
Integer val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(Integer val) { this.val = val; }
TreeNode(Integer val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; }
}
public static class Tree {
TreeNode root;
Tree() { this.root = null; }
}
private static TreeNode creator(Integer[] values, TreeNode root, int i, int n) {
if (n==0) return null;
if (i<n) {
TreeNode temp = new TreeNode(values[i]);
root = temp;
root.left = creator(values, root.left, 2*i+1, n);
root.right = creator(values, root.right, 2*i+2, n);
}
return root;
}
public static void createTree(Tree tree, Integer[] inputs) {
tree.root = creator(inputs, tree.root, 0, inputs.length);
}
public static void showTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
ArrayList<Integer> c = new ArrayList<>();
if (root == null) { System.out.println("Empty !"); return; }
q.add(root);
q.add(null);
while (!q.isEmpty()) {
TreeNode t = q.poll();
if (t==null) {
result.add(c);
c = new ArrayList<>();
if (q.size() > 0) q.add(null);
} else {
c.add(t.val);
if (t.left!=null) q.add(t.left);
if (t.right!=null) q.add(t.right);
}
}
System.out.print("["); for (var x : result) {
System.out.print("["); for (var y : x) {
if (y==null) { System.out.print("NULL,"); continue; }
System.out.print(y+",");
} System.out.print("\b],");
} System.out.println("\b]");
}
// SOLUTION
private boolean traverse(TreeNode root, TreeNode min, TreeNode max) {
if (root==null) return true;
if (min!=null && root.val<=min.val) return false;
if (max!=null && root.val>=max.val) return false;
boolean l = traverse(root.left, min, root);
boolean r = traverse(root.right, root, max);
return l && r;
}
public boolean isValidBST(TreeNode root) {
return traverse(root, null, null);
}
public static void main(String[] args) {
Solution o = new Solution();
Tree tree = new Tree();
// INPUT
Integer[] tn = {2,1,3};
createTree(tree, tn);
// OUTPUT
var result = o.isValidBST(tree.root);
System.out.println(result);
}
}