-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_23.java
More file actions
68 lines (57 loc) · 1.94 KB
/
Copy pathBT_Problem_23.java
File metadata and controls
68 lines (57 loc) · 1.94 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
package trees.binaryTree;
//Problem Title => Check if all leaf nodes are at same level or not
public class BT_Problem_23 {
// Class to create Node of tree
static class Node{
int data;
Node left,right;
Node(int item){
this.data = item;
left = right = null;
}
}
// static leaf class
static class Leaf{
int leafLevel = 0;
}
Node root;
Leaf myLevel = new Leaf();
// recursive function to check
boolean checkUtil(Node node, int level, Leaf leafLevel){
//base case
if(node == null)
return true;
if(node.left == null && node.right == null){
// When a leaf node is found first time
if (leafLevel.leafLevel == 0){
// Set first found leaf's level
leafLevel.leafLevel = level;
return true;
}
// If this is not first leaf node, compare its level with first leaf's level
return (level == leafLevel.leafLevel);
}
// If this node is not leaf, recursively check left and right subtrees
return checkUtil(node.left, level + 1, leafLevel)
&& checkUtil(node.right, level + 1, leafLevel);
}
// Function to check which uses checkUtil()
boolean check(Node node){
int level = 0;
return checkUtil(node, level, myLevel);
}
// Driver Function
public static void main(String[] args) {
BT_Problem_23 tree = new BT_Problem_23();
tree.root = new Node(12);
tree.root.left = new Node(5);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(9);
tree.root.left.left.left = new Node(1);
tree.root.left.right.left = new Node(1);
if (tree.check(tree.root))
System.out.println("Leaves are at same level");
else
System.out.println("Leaves are not at same level");
}
}