Repository navigation
Expand file tree
/
Copy pathLCA.java
More file actions
112 lines (88 loc) · 1.82 KB
/
Copy pathLCA.java
File metadata and controls
112 lines (88 loc) · 1.82 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
import java.io.*;
import java.util.*;
public class LCA{
static class Node{
int data;
Node left;
Node right;
Node(int data)
{
this.data = data;
this.left= null;
this.right = null;
}
}
static Node root; // root node
static void create(){
Scanner sc = new Scanner(System.in);
int x;
Node p, t;
Queue<Node> q = new LinkedList<>();
System.out.println("Enter the root ");
x = sc.nextInt();
root = new Node(x);
q.add(root);
while(!q.isEmpty()){
p = q.poll();
System.out.println("Enter the left node of "+p.data);
x = sc.nextInt();
if(x!=-1){
t = new Node(x);
p.left = t;
q.add(t);
}
System.out.println("Enter the right node of "+p.data);
x = sc.nextInt();
if(x!=-1){
t = new Node(x);
p.right = t;
q.add(t);
}
}
}
static Node Lc(int n1, int n2){
Node lk = Lca(root, n1, n2);
return lk;
}
static int find(Node root, int x, int y){
Node temp = Lc(x, y);
int lh = height(temp.left);
int rh = height(temp.right);
int res = lh+rh;
return res;
}
static Node Lca(Node n, int n1, int n2){
if(n==null){
return null;
}
if(n.data == n1 || n.data == n2){
return n;
}
Node lf = Lca(n.left, n1, n2);
Node rf = Lca(n.right, n1 , n2);
if(lf!=null && rf!=null){
return n;
}
if(lf!=null){
return lf;
}
else{
return rf;
}
}
static int height(Node root){
if(root==null){
return 0;
}
int lt = height(root.left);
int rt = height(root.right);
int res = Math.max(lt, rt)+1;
return res;
}
public static void main(String[] args) {
create();
int xx = 4;
int yy = 6;
System.out.println(find(root, xx, yy));
}
}