-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTrees2.java
119 lines (112 loc) · 3.43 KB
/
binaryTrees2.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.*;
public class binaryTrees2 {
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
};
// Kth level of Tree
public static void klevel(Node root, int level, int k) {
if (root == null) {
return;
}
if (level == k) {
System.out.println(root.data);
return;
}
klevel(root.left, level + 1, k);
klevel(root.right, level + 1, k);
}
// Lowest common ancestor, TIME COMPLEXITY = LINEAR, Approach 1
public static boolean getPath(Node root, int n, ArrayList<Node> path) {
if (root == null) {
return false;
}
path.add(root);
if (root.data == n) {
return true;
}
boolean foundLeft = getPath(root.left, n, path);
boolean foundRight = getPath(root.right, n, path);
if (foundLeft || foundRight) {
return true;
}
// if node not found anywhere, remove it
path.remove(path.size() - 1);
return false;
}
public static Node lca(Node root, int n1, int n2) {
// find path from root to node
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
getPath(root, n1, path1);
getPath(root, n2, path2);
// calculate lowest common ancestor lca
int i = 0;
for (; i < path1.size() && i < path2.size(); i++) {
if (path1.get(i) != path2.get(i)) {
break;
}
}
Node lca = path1.get(i - 1);
return lca;
}
//APPROACH 2
public static Node lca2(Node root, int n1, int n2) {
if (root == null || root.data == n1 || root.data == n2) {
return root;
}
Node leftLca = lca2(root.left, n1, n2);
Node rightLca = lca2(root.right, n1, n2);
// case where leftLCA= valid but rightLCA=null
if (rightLca == null) {
return leftLca;
}
if (leftLca == null) {
return rightLca;
}
// case where no null was returned from any side
// then we have found lca
return root;
}
//MINIMUM DISTANCE bw TWO NODES
public static int lcaDist(Node root, int n){
if(root == null){
return -1;
}
if(root.data==n){
return 0;
}
int leftDist = lcaDist(root.left,n);
int rightDist=lcaDist(root.right, n);
//nodes were not found
if(leftDist == -1 && rightDist == -1){
return -1;
}else if(leftDist == -1){ //node in right subtree
return rightDist + 1;
}else{
return leftDist+1; //node found in left subtree
}
}
public static int minDistance(Node root, int n1, int n2){
Node lca= lca2(root, n1, n2);
int dist1 = lcaDist(lca,n1);
int dist2=lcaDist(lca, n2);
return dist1+dist2;
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.right = new Node(5);
klevel(root, 1, 2);
System.out.println("LCA=" + lca2(root, 2, 3).data);
System.out.println("Minimum distance= " +minDistance(root, 2,3));
}
}