forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtreeCountNodes.java
More file actions
45 lines (40 loc) · 781 Bytes
/
Copy pathbtreeCountNodes.java
File metadata and controls
45 lines (40 loc) · 781 Bytes
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
class Node
{
int data;
Node left;
Node right;
Node(int data)
{
this.data=data;
left=right=null;
}
}
class BinaryTree{
Node root;
BinaryTree()
{
root = null;
}
BinaryTree(int data)
{
this.root=new Node(data);
}
int count(Node head)
{
if(head==null)
{
return 0;
}
return 1+count(head.left)+count(head.right);
}
}
class btreeCountNodes{
public static void main(String[] args) {
BinaryTree bt=new BinaryTree(2);
bt.root.left=new Node(3);
bt.root.right=new Node(5);
bt.root.left.right=new Node(9);
bt.root.right.left=new Node(7);
System.out.println(bt.count(bt.root));
}
}