Repository navigation
Expand file tree
/
Copy pathMainClass.java
More file actions
61 lines (36 loc) · 1.42 KB
/
Copy pathMainClass.java
File metadata and controls
61 lines (36 loc) · 1.42 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
package binarySearchTree2;
public class MainClass {
public boolean isValidBST(TreeNode root) {
// This code is not correct
// if(root == null) return true;
// if((root.left != null && root.left.val >= root.val) &&
// (root.right != null && root.right.val <= root.val)) return false;
// return isValidBST(root.left) && isValidBST(root.right);
// incorrect code ends here
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
boolean isValidBST(TreeNode root, long minValue, long maxValue) {
if(root == null) return true;
if(root.val <= minValue || root.val >= maxValue) return false;
return isValidBST(root.left, minValue, root.val) &&
isValidBST(root.right, root.val, maxValue);
}
int distance(Node root, int a) {
if(root.data == a) return 0;
if(a < root.data) return 1+ distance(root.left, a);
return 1+distance(root.right, a);
}
int findDist(Node root, int a, int b) {
if(root == null) return 0;
if(a < root.data && b < root.data) {
return findDist(root.left, a, b);
}
if(a > root.data && b > root.data) {
return findDist(root.right, a, b);
}
return distance(root, a) + distance(root, b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}