Skip to content

Commit 756aa1e

Browse files
committed
Added 3 tree solutions
1 parent 2850872 commit 756aa1e

21 files changed

+166
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class GfG {
2+
3+
boolean isBalanced(Node root) {
4+
if (root == null) {
5+
return true;
6+
}
7+
8+
if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) {
9+
return false;
10+
}
11+
12+
return isBalanced(root.left) && isBalanced(root.right);
13+
}
14+
15+
int getHeight(Node root) {
16+
if (root == null) {
17+
return 0;
18+
}
19+
20+
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
21+
}
22+
}
23+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*class Node
2+
{
3+
int data;
4+
Node left, right;
5+
6+
Node(int key)
7+
{
8+
data = key;
9+
left = right = null;
10+
}
11+
12+
}*/
13+
class GfG {
14+
public static int findMaxforKey(Node node, int val, int size) {
15+
int[] max = {-1};
16+
findMaxForKeyHelper(node, max, val);
17+
18+
return max[0];
19+
}
20+
21+
private static void findMaxForKeyHelper(Node node, int[] max, int val) {
22+
if (node == null) {
23+
return;
24+
}
25+
26+
if (node.data == val) {
27+
max[0] = node.data;
28+
return;
29+
}
30+
31+
if (node.data < val) {
32+
max[0] = Math.max(max[0], node.data);
33+
findMaxForKeyHelper(node.right, max, val);
34+
}
35+
else {
36+
findMaxForKeyHelper(node.left, max, val);
37+
}
38+
}
39+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*Complete the function below
2+
Node is as follows:
3+
class Node{
4+
int data;
5+
Node left,right;
6+
Node(int d){
7+
data=d;
8+
left=right=null;
9+
}
10+
}
11+
*/
12+
class GfG {
13+
public Node inorderSuccessor(Node root,Node k) {
14+
k = findK(root, k.data);
15+
if (k.right != null) {
16+
k = k.right;
17+
while (k.left != null) {
18+
k = k.left;
19+
}
20+
21+
return k;
22+
}
23+
24+
return lastLeft(root, k);
25+
}
26+
27+
private Node findK(Node root, int k) {
28+
if (root == null) {
29+
return null;
30+
}
31+
if (root.data == k) {
32+
return root;
33+
}
34+
else if (root.data < k) {
35+
return findK(root.right, k);
36+
}
37+
else {
38+
return findK(root.left, k);
39+
}
40+
}
41+
42+
private Node lastLeft(Node root, Node k) {
43+
Node last = new Node(-1);
44+
45+
while (root != null && root.data != k.data) {
46+
if (root.data < k.data) {
47+
root = root.right;
48+
}
49+
else if (root.data > k.data) {
50+
last = root;
51+
root = root.left;
52+
}
53+
}
54+
55+
return last;
56+
}
57+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class GfG {
2+
int maxDiff(Node root) {
3+
int[] ans = {Integer.MIN_VALUE};
4+
maxDiffHelper(root, ans);
5+
6+
return ans[0];
7+
}
8+
9+
private int maxDiffHelper(Node node, int[] ans) {
10+
if (node == null) {
11+
return Integer.MAX_VALUE;
12+
}
13+
14+
int minLeft = maxDiffHelper(node.left, ans);
15+
int minRight = maxDiffHelper(node.right, ans);
16+
17+
int diff = Math.max(node.data - minLeft, node.data - minRight);
18+
19+
if(diff > ans[0]){
20+
ans[0] = diff;
21+
}
22+
23+
return Math.min(node.data, Math.min(minLeft, minRight));
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*node class of the binary ssearch tree
2+
class Node
3+
{
4+
int data;
5+
Node left, right;
6+
Node(int key)
7+
{
8+
data = key;
9+
left = right = null;
10+
}
11+
}*/
12+
class GfG {
13+
public static int sumOfLeafNodes(Node root) {
14+
if(root == null)
15+
return 0;
16+
17+
if(root.left == null && root.right == null)
18+
return root.data;
19+
20+
return sumOfLeafNodes(root.left)+sumOfLeafNodes(root.right);
21+
}
22+
}

0 commit comments

Comments
 (0)