Skip to content

Commit fe6823c

Browse files
Add files via upload
1 parent 5063112 commit fe6823c

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

BST 1/BST TO LL.txt

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class PassNode<T>
2+
{
3+
T head;
4+
T tail;
5+
}
6+
public class Solution {
7+
8+
/*
9+
* Binary Tree Node class
10+
*
11+
* class BinaryTreeNode<T> { T data; BinaryTreeNode<T> left; BinaryTreeNode<T>
12+
* right;
13+
*
14+
* public BinaryTreeNode(T data) { this.data = data; } }
15+
*/
16+
17+
/*
18+
* LinkedList Node Class
19+
*
20+
*
21+
* class LinkedListNode<T> { T data; LinkedListNode<T> next;
22+
*
23+
* public LinkedListNode(T data) { this.data = data; } }
24+
*/
25+
26+
public static LinkedListNode<Integer> constructLinkedList(BinaryTreeNode<Integer> root) {
27+
PassNode<LinkedListNode<Integer>> passNode= constructLinkedListHelper(root);
28+
return passNode.head;
29+
}
30+
31+
public static PassNode<LinkedListNode<Integer>> constructLinkedListHelper(BinaryTreeNode<Integer> root)
32+
{
33+
if(root==null)
34+
{
35+
return new PassNode<LinkedListNode<Integer>>();
36+
}
37+
38+
if (root.left==null && root.right==null)
39+
{
40+
PassNode<LinkedListNode<Integer>> passNode = new PassNode<LinkedListNode<Integer>>();
41+
passNode.head=new LinkedListNode<Integer>(root.data);
42+
passNode.tail=null;
43+
return passNode;
44+
}
45+
46+
PassNode<LinkedListNode<Integer>> passNodeLeft = constructLinkedListHelper(root.left);
47+
PassNode<LinkedListNode<Integer>> passNodeRight = constructLinkedListHelper(root.right);
48+
PassNode<LinkedListNode<Integer>> passNode = new PassNode<LinkedListNode<Integer>>();
49+
LinkedListNode<Integer> newNode=new LinkedListNode<Integer>(root.data);
50+
51+
if (passNodeLeft.head==null)
52+
{
53+
passNode.head=newNode;
54+
//passNode.tail=newNode;
55+
}
56+
else
57+
{
58+
passNode.head=passNodeLeft.head;
59+
}
60+
61+
if(passNodeLeft.tail==null)
62+
{
63+
passNode.head.next=newNode;
64+
passNode.tail=newNode;
65+
}
66+
else
67+
{
68+
passNodeLeft.tail.next=newNode;
69+
passNode.tail=newNode;
70+
}
71+
72+
if (passNodeRight.head!=null)
73+
{
74+
passNode.tail.next=passNodeRight.head;
75+
passNode.tail=passNodeRight.head;
76+
}
77+
78+
if(passNodeRight.tail!=null)
79+
{
80+
passNode.tail=passNodeRight.tail;
81+
}
82+
83+
return passNode;
84+
85+
}
86+
}

BST 1/CONSTRUCT BST.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
3+
/* Binary Tree Node class
4+
*
5+
* class BinaryTreeNode<T> {
6+
T data;
7+
BinaryTreeNode<T> left;
8+
BinaryTreeNode<T> right;
9+
10+
public BinaryTreeNode(T data) {
11+
this.data = data;
12+
}
13+
}
14+
*/
15+
16+
public static BinaryTreeNode<Integer> SortedArrayToBST(int[] arr, int n){
17+
return SortedArrayToBSTHelper(arr,0,n-1);
18+
19+
}
20+
21+
public static BinaryTreeNode<Integer> SortedArrayToBSTHelper(int[] arr, int si, int ei){
22+
if (si>ei)
23+
return null;
24+
25+
int mid=(si+ei)/2;
26+
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(arr[mid]);
27+
28+
root.left=SortedArrayToBSTHelper(arr,si,mid-1);
29+
root.right=SortedArrayToBSTHelper(arr,mid+1,ei);
30+
return root;
31+
32+
}
33+
}

BST 1/ELEMENTS BETWEEN K1 AND K2.txt

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.*;
2+
public class Solution {
3+
4+
/* Binary Tree Node class
5+
*
6+
* class BinaryTreeNode<T> {
7+
T data;
8+
BinaryTreeNode<T> left;
9+
BinaryTreeNode<T> right;
10+
11+
public BinaryTreeNode(T data) {
12+
this.data = data;
13+
}
14+
}
15+
*/
16+
17+
private static ArrayList<Integer> arr = new ArrayList<Integer>();
18+
19+
public static void elementsInRangeK1K2(BinaryTreeNode<Integer> root,int k1,int k2)
20+
{
21+
elementsInRangeK1K2Helper(root,k1,k2);
22+
Collections.sort(arr);
23+
for (int i:arr)
24+
System.out.print(i+" ");
25+
}
26+
public static void elementsInRangeK1K2Helper(BinaryTreeNode<Integer> root,int k1,int k2){
27+
28+
//Base case - if root=null
29+
if (root==null)
30+
return;
31+
32+
int rootData=root.data;
33+
if (rootData<k1)
34+
{
35+
elementsInRangeK1K2Helper(root.right,k1,k2);
36+
}
37+
else if (rootData>k2)
38+
{
39+
elementsInRangeK1K2Helper(root.left,k1,k2);
40+
}
41+
else
42+
{
43+
//System.out.print(rootData+" ");
44+
arr.add(rootData);
45+
elementsInRangeK1K2Helper(root.right,k1,k2);
46+
elementsInRangeK1K2Helper(root.left,k1,k2);
47+
}
48+
49+
}
50+
51+
52+
}

BST 1/LCA OF BST.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Solution {
2+
3+
/*
4+
* Binary Tree Node class
5+
*
6+
* class BinaryTreeNode<T> { T data; BinaryTreeNode<T> left; BinaryTreeNode<T> right;
7+
*
8+
* public BinaryTreeNode(T data) { this.data = data; } }
9+
*/
10+
11+
12+
13+
public static int getLCA(BinaryTreeNode<Integer> root, int n1, int n2) {
14+
if (root == null)
15+
return -1;
16+
17+
// If both n1 and n2 are smaller than root, then LCA lies in left
18+
if (root.data > n1 && root.data > n2)
19+
return getLCA(root.left, n1, n2);
20+
21+
// If both n1 and n2 are greater than root, then LCA lies in right
22+
if (root.data < n1 && root.data < n2)
23+
return getLCA(root.right, n1, n2);
24+
25+
return root.data;
26+
27+
28+
}
29+
}

BST 1/SEARCH IN BST.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Solution {
2+
3+
/*
4+
* Binary Tree Node class
5+
*
6+
* class BinaryTreeNode<T> { T data; BinaryTreeNode<T> left; BinaryTreeNode<T> right;
7+
*
8+
* public BinaryTreeNode(T data) { this.data = data; } }
9+
*/
10+
11+
12+
public static boolean searchInBST(BinaryTreeNode<Integer> root, int k) {
13+
if (root==null)
14+
{
15+
return false;
16+
}
17+
18+
int rootData=root.data;
19+
if (k<rootData)
20+
{
21+
return searchInBST(root.left,k);
22+
}
23+
else if(k>rootData)
24+
{
25+
return searchInBST(root.right,k);
26+
}
27+
else
28+
{
29+
return true;
30+
}
31+
32+
33+
}
34+
}

0 commit comments

Comments
 (0)