Skip to content

Commit 78e0108

Browse files
committed
Added 7 tree solutions
1 parent db3066f commit 78e0108

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class ChildrenSumParent {
2+
public static int sum(Node root){
3+
if(root==null) {
4+
return 0;
5+
}
6+
7+
if(root.left==null && root.right==null) {
8+
return root.data;
9+
}
10+
11+
return sum(root.left)+sum(root.right);
12+
}
13+
14+
public static int isSumProperty(Node node) {
15+
if(node==null) {
16+
return 1;
17+
}
18+
19+
if(node.left==null && node.right==null) {
20+
return 1;
21+
}
22+
23+
int s=sum(node.left)+sum(node.right);
24+
25+
if(s==node.data) {
26+
return Math.min(isSumProperty(node.left), isSumProperty(node.right));
27+
}
28+
else {
29+
return 0;
30+
}
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class CountNonLeafNodesInTree {
2+
int countNonLeafNodes(Node node) {
3+
if (node == null) {
4+
return 0;
5+
}
6+
7+
if (node.left == null && node.right == null) {
8+
return 0;
9+
}
10+
11+
return 1 + countNonLeafNodes(node.left) + countNonLeafNodes(node.right);
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* A Binary Tree node
2+
class Node {
3+
int data;
4+
Node left, right;
5+
Node(int item) {
6+
data = item;
7+
left = right = null;
8+
}
9+
} */
10+
class GfG {
11+
void inOrder(Node root)
12+
{
13+
if (root == null) {
14+
return;
15+
}
16+
17+
inOrder(root.left);
18+
System.out.print(root.data + " ");
19+
inOrder(root.right);
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class LevelOfNodeInBinaryTree {
2+
public static int getLevel(Node root, int key) {
3+
int level = 1;
4+
Queue<Node> queue = new LinkedList<>();
5+
6+
queue.add(root);
7+
8+
while (!queue.isEmpty()) {
9+
int size = queue.size();
10+
11+
while (size-- > 0) {
12+
Node temp = queue.remove();
13+
if (temp.data == key) {
14+
return level;
15+
}
16+
17+
if (temp.left != null) {
18+
queue.add(temp.left);
19+
}
20+
21+
if (temp.right != null) {
22+
queue.add(temp.right);
23+
}
24+
}
25+
26+
level++;
27+
}
28+
29+
return -1;
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class MaxMinBinaryTree {
2+
public static int findMax(Node root) {
3+
if (root == null) {
4+
return Integer.MIN_VALUE;
5+
}
6+
7+
return Math.max(root.data, Math.max(findMax(root.left), findMax(root.right)));
8+
}
9+
public static int findMin(Node root) {
10+
if (root == null) {
11+
return Integer.MAX_VALUE;
12+
}
13+
14+
return Math.min(root.data, Math.min(findMin(root.left), findMin(root.right)));
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SumOfLeftLeafNodes {
2+
public int sum = 0;
3+
public int leftLeafSum(Node root) {
4+
updateSum(root, false);
5+
return sum;
6+
}
7+
8+
public void updateSum(Node node, boolean isLeft) {
9+
if (node == null) {
10+
return;
11+
}
12+
13+
if (node.left == null && node.right == null && isLeft) {
14+
sum += node.data;
15+
}
16+
17+
updateSum(node.left, true);
18+
updateSum(node.right, false);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SumOfRightLeafNodes {
2+
public int sum = 0;
3+
public int rightLeafSum(Node root) {
4+
updateSum(root, false);
5+
return sum;
6+
}
7+
8+
public void updateSum(Node node, boolean isRight) {
9+
if (node == null) {
10+
return;
11+
}
12+
13+
if (node.left == null && node.right == null && isRight) {
14+
sum += node.data;
15+
}
16+
17+
updateSum(node.left, false);
18+
updateSum(node.right, true);
19+
}
20+
}

0 commit comments

Comments
 (0)