forked from Anuj-Kumar-Sharma/Java-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
125 lines (91 loc) · 3.1 KB
/
Copy pathMainClass.java
File metadata and controls
125 lines (91 loc) · 3.1 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
package binaryTree3;
import java.util.*;
import binaryTree2.MainClass.TreeNode;
public class MainClass {
class Node
{
int data;
Node left, right;
Node(int key)
{
data = key;
left = right = null;
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
// https://practice.geeksforgeeks.org/problems/ancestors-in-binary-tree/1
public static ArrayList<Integer> Ancestors(Node root, int target) {
ArrayList<Integer> ans = new ArrayList<>();
isPresent(root, target, ans);
return ans;
}
public static boolean isPresent(Node root, int target, ArrayList<Integer> ans) {
if(root == null) return false;
if(root.data == target || isPresent(root.left, target, ans) || isPresent(root.right, target, ans)) {
ans.add(root.data);
return true;
}
return false;
}
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/submissions/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return null;
if(root.val == p.val || root.val == q.val) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(right == null) return left;
if(left == null) return right;
return root;
}
// public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// ArrayList<TreeNode> l1 = new ArrayList<>();
// ArrayList<TreeNode> l2 = new ArrayList<>();
// isPresent(root, p.val, l1);
// isPresent(root, q.val, l2);
// int i = l1.size()-1;
// int j = l2.size()-1;
// while(i >= 0 && j >= 0 && l1.get(i).val == l2.get(j).val) {
// i--;
// j--;
// }
// if(i == l1.size()-1) return null;
// return l1.get(i+1);
// }
// public boolean isPresent(TreeNode root, int target, ArrayList<TreeNode> ans) {
// if(root == null) return false;
// if(root.val == target || isPresent(root.left, target, ans) || isPresent(root.right, target, ans)) {
// ans.add(root);
// return true;
// }
// return false;
// }
// https://practice.geeksforgeeks.org/problems/binary-tree-to-dll/1
Node prev = null, head = null;
Node bToDLL(Node root) {
bToDLLUtil(root);
return head;
}
void bToDLLUtil(Node root) {
if(root == null) return;
bToDLLUtil(root.left);
if(prev == null) {
head = root;
} else {
root.left = prev;
prev.right = root;
}
prev = root;
bToDLLUtil(root.right);
}
}