Skip to content

Commit 6cb1162

Browse files
committed
count nodes
1 parent 5d1e291 commit 6cb1162

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Trees/btreeCountNodes.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
class Node
3+
{
4+
int data;
5+
Node left;
6+
Node right;
7+
Node(int data)
8+
{
9+
this.data=data;
10+
left=right=null;
11+
}
12+
}
13+
14+
class BinaryTree{
15+
Node root;
16+
BinaryTree()
17+
{
18+
root = null;
19+
}
20+
BinaryTree(int data)
21+
{
22+
this.root=new Node(data);
23+
}
24+
int count(Node head)
25+
{
26+
27+
if(head==null)
28+
{
29+
return 0;
30+
}
31+
return 1+count(head.left)+count(head.right);
32+
}
33+
}
34+
35+
class btreeCountNodes{
36+
public static void main(String[] args) {
37+
38+
BinaryTree bt=new BinaryTree(2);
39+
bt.root.left=new Node(3);
40+
bt.root.right=new Node(5);
41+
bt.root.left.right=new Node(9);
42+
bt.root.right.left=new Node(7);
43+
System.out.println(bt.count(bt.root));
44+
}
45+
}

0 commit comments

Comments
 (0)