Skip to content

Commit 8ed9e6b

Browse files
committed
First commit
1 parent 24b1cd6 commit 8ed9e6b

12 files changed

+286
-0
lines changed

Array/Power of 2.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
5+
class GFG {
6+
public static void main (String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int t = sc.nextInt();
9+
while (t-- != 0) {
10+
long n = sc.nextLong();
11+
if (n == 0) {
12+
System.out.println("NO");
13+
continue;
14+
}
15+
if ((n & (n - 1)) == 0) {
16+
System.out.println("YES");
17+
}
18+
else {
19+
System.out.println("NO");
20+
}
21+
}
22+
}
23+
}

BST/Check for BST.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*package whatever //do not write package name here */
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class Node {
8+
int data;
9+
Node left;
10+
Node right;
11+
Node (int x) {
12+
data = x;
13+
left = right = null;
14+
}
15+
}
16+
17+
18+
19+
class GFG {
20+
public static void main (String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
int t = sc.nextInt();
23+
while (t-- != 0) {
24+
Node root = null;
25+
int n = sc.nextInt();
26+
while (n-- != 0) {
27+
int num = sc.nextInt();
28+
root = createTree(root, num);
29+
}
30+
int cnt = sc.nextInt();
31+
inOrder(root, cnt);
32+
}
33+
}
34+
static Node createTree(Node root, int x) {
35+
if (root == null) {
36+
root = new Node(x);
37+
}
38+
else if (x < root.data) {
39+
root.left = createTree(root.left, x);
40+
}
41+
else if (x > root.data) {
42+
root.right = createTree(root.right, x);
43+
}
44+
return root;
45+
}
46+
47+
static void inOrder(Node root, int cnt) {
48+
// if (a.)
49+
// if (root == null) {
50+
// return;
51+
// }
52+
// inOrder(root.left);
53+
// a[cnt++] = root.data;
54+
// inOrder(root.right);
55+
ArrayList<Integer> al = new ArrayList<>();
56+
Stack<Node> st = new Stack<>();
57+
Node curr = root;
58+
while (!st.empty() || curr != null) {
59+
while (curr != null) {
60+
st.push(curr);
61+
curr = curr.left;
62+
}
63+
curr = st.pop();
64+
al.add(curr.data);
65+
curr = curr.right;
66+
}
67+
int sum = 0;
68+
for (int i = 0; i < cnt; i++) {
69+
sum += al.get(i);
70+
}
71+
System.out.println(sum);
72+
}
73+
}

BST/Deletion of Node in BST.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
int minV(Node root) {
2+
int minV = root.key;
3+
while (root.left != null) {
4+
5+
root = root.left;
6+
minV = root.key;
7+
8+
}
9+
return minV;
10+
}
11+
12+
Node deleteNode(Node root, int key)
13+
{
14+
// Your code here
15+
if (root == null) {
16+
return root;
17+
}
18+
if (key < root.key) {
19+
root.left = deleteNode(root.left, key);
20+
}
21+
else if (key > root.key) {
22+
root.right = deleteNode(root.right, key);
23+
}
24+
else {
25+
if (root.right == null) {
26+
root = root.left;
27+
}
28+
else if (root.left == null) {
29+
root = root.right;
30+
}
31+
else {
32+
root.key = minV(root.right);
33+
root.right = deleteNode(root.right, root.key);
34+
}
35+
}
36+
return root;
37+
}

BST/Find Element in BST.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
boolean search(Node root, int x)
2+
{
3+
if (root == null) {
4+
return false;
5+
}
6+
else if (root.data == x) {
7+
return true;
8+
}
9+
else if (x < root.data) {
10+
return search(root.left, x);
11+
}
12+
else {
13+
return search(root.right, x);
14+
}
15+
}

BST/Inorder Traversal and BST.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*package whatever //do not write package name here */
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class GFG {
8+
public static void main (String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
int t = sc.nextInt();
11+
int f = 0;
12+
while (t-- != 0) {
13+
int n = sc.nextInt();
14+
int a[] = new int[n];
15+
for (int i = 0; i < n; i++) {
16+
a[i] = sc.nextInt();
17+
}
18+
f = 0;
19+
for (int i = 1; i < n; i++) {
20+
if (a[i - 1] >= a[i]) {
21+
// System.out.println(a[i - 1] + " > " + a[i]);
22+
f = 1;
23+
break;
24+
}
25+
}
26+
if (f == 0) {
27+
System.out.println(1);
28+
}
29+
else {
30+
System.out.println(0);
31+
}
32+
}
33+
}
34+
}

BST/Insertion of Node in BST.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Node insert(Node root, int data)
2+
{
3+
if (root == null) {
4+
root = new Node(data);
5+
return root;
6+
}
7+
else if (data < root.data) {
8+
root.left = insert(root.left, data);
9+
}
10+
else if (data > root.data) {
11+
root.right = insert(root.right, data);
12+
}
13+
return root;
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Node lca(Node node, int n1, int n2)
2+
{
3+
if (n1 < node.data && n2 > node.data) {
4+
return node;
5+
}
6+
else if (n1 < node.data && n2 < node.data) {
7+
return lca(node.left, n1, n2);
8+
}
9+
else if (n1 > node.data && n2 > node.data) {
10+
return lca(node.right, n1, n2);
11+
}
12+
return node;
13+
}

BST/Minimum Element in BST.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int minValue(Node node)
2+
{
3+
if (node == null) {
4+
return -1;
5+
}
6+
while (node.left != null) {
7+
node = node.left;
8+
}
9+
return node.data;
10+
}

BST/Sum of Leaf Nodes.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class GfG
2+
{
3+
static int sum = 0;
4+
static void inOrder(Node root) {
5+
if (root == null) {
6+
return;
7+
}
8+
if (root.left == null && root.right == null) {
9+
sum += root.data;
10+
}
11+
12+
inOrder(root.left);
13+
inOrder(root.right);
14+
}
15+
16+
public static int sumOfLeafNodes(Node root)
17+
{
18+
sum = 0;
19+
inOrder(root);
20+
return sum;
21+
/*if(root == null)
22+
return 0;
23+
24+
if(root.left == null && root.right == null)
25+
return root.data;
26+
27+
return sumOfLeafNodes(root.left)+sumOfLeafNodes(root.right);*/
28+
}
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int detectLoop(Node head) {
2+
Set<Node> s = new HashSet<>();
3+
while (head != null) {
4+
if (s.contains(head)) {
5+
return 1;
6+
}
7+
s.add(head);
8+
head = head.next;
9+
}
10+
return 0;
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public boolean isIdentical (Node head1, Node head2){
2+
while (head1!= null && head2 != null) {
3+
if (head1.data != head2.data) {
4+
return false;
5+
}
6+
head1 = head1.next;
7+
head2 = head2.next;
8+
}
9+
return true;
10+
}

Tree/Sum of Nodes in Binary Tree.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class GfG{
2+
public int sumBT(Node root){
3+
Stack<Node> st = new Stack<>();
4+
int sum = 0;
5+
Node curr = root;
6+
while (st.size() > 0 || curr != null) {
7+
while (curr != null) {
8+
st.push(curr);
9+
curr = curr.left;
10+
}
11+
curr = st.pop();
12+
sum += curr.data;
13+
curr = curr.right;
14+
}
15+
return sum;
16+
}
17+
}

0 commit comments

Comments
 (0)