-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCountFullNodes.java
More file actions
41 lines (39 loc) · 1.04 KB
/
Copy pathCountFullNodes.java
File metadata and controls
41 lines (39 loc) · 1.04 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
//Question: Count all the full nodes(nodes whose left and right children are not null)
//initializing node class
class Node{
int key;
Node left,right;
public Node(int data) {
key=data;
left=right=null;
}
}
public class CountFullNodes{
Node root;
CountFullNodes(){
root=null;
}
//recursive function to get the count of full nodes
int getFullCount(Node node) {
if(node==null)
return 0;
int count=0;
if(node.left!=null && node.right!=null)
count++;
count+=(getFullCount(node.left)+getFullCount(node.right));
return count;
}
public static void main(String[] args) {
CountFullNodes bs=new CountFullNodes();
bs.root=new Node(1);
bs.root.left = new Node(7);
bs.root.right = new Node(3);
bs.root.left.right = new Node(6);
bs.root.left.right.left = new Node(2);
bs.root.left.right.right = new Node(4);
bs.root.right.right = new Node(9);
bs.root.right.right.left = new Node(11);
bs.root.right.right.right = new Node(10);
System.out.println(bs.getFullCount(bs.root));
}
}