-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree3.java
More file actions
171 lines (149 loc) · 4.06 KB
/
BinaryTree3.java
File metadata and controls
171 lines (149 loc) · 4.06 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*/Q1.: Kth level of a tree ; Tc=O(n)
public class BinaryTree3 {
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public static void kLevel(Node root,int level,int k){
if(root == null){
return ;
}
if(level==k){
System.out.print(root.data+" ");
return;
}
kLevel(root.left,level+1,k);
kLevel(root.right,level+1,k);
}
public static void main(String[] args) {
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
Node root = new Node(1);
root.left= new Node(2);
root.right= new Node(3);
root.left.left= new Node(4);
root.left.right= new Node(5);
root.right.left= new Node(6);
root.right.right= new Node(7);
int k=2;
kLevel(root, 1, 2);
}
}
*/
//Q2.: Lowest common ancestor
/*/approach 1
import java.util.*;
public class BinaryTree3 {
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
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;
}
path.remove(path.size()-1);
return false;
}
public static Node lca(Node root,int n1,int n2){
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
getPath(root,n1,path1);
getPath(root,n2,path2);
//last common ancestor
int i=0;
for(;i<path1.size() && i<path2.size();i++){
if(path1.get(i)!=path2.get(i)){
break;
}
}
//last equal node -> i-1th
Node lca = path1.get(i-1);
return lca;
}
public static void main(String[] args) {
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
Node root = new Node(1);
root.left= new Node(2);
root.right= new Node(3);
root.left.left= new Node(4);
root.left.right= new Node(5);
root.right.left= new Node(6);
root.right.right= new Node(7);
int n1=4;
int n2=7;
System.out.println(lca(root, n1, n2).data);
}
}*/
//approach 2 ; Time complexity = O(n) & space complexity =O(n)
public class BinaryTree3 {
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public static Node lca(Node root,int n1,int n2){
if(root ==null || root.data ==n1 || root.data == n2){
return root;
}
Node leftLca = lca(root.left,n1,n2);
Node rightLca = lca(root.right,n1,n2);
if(rightLca==null){
return leftLca; //leftlca = valid & rightLca = null
}
if(leftLca==null){
return rightLca; // rightlca = valid & leftLca=null
}
return root;
}
public static void main(String[] args) {
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
Node root = new Node(1);
root.left= new Node(2);
root.right= new Node(3);
root.left.left= new Node(4);
root.left.right= new Node(5);
root.right.left= new Node(6);
root.right.right= new Node(7);
int n1=4;
int n2=7;
System.out.println(lca(root, n1, n2).data);
}
}