size(tree)
1. If tree is empty then return 0
2. Else
(a) Get the size of left subtree recursively i.e., call
size( tree->left-subtree)
(a) Get the size of right subtree recursively i.e., call
size( tree->right-subtree)
(c) Calculate size of the tree as following:
tree_size = size(left-subtree) + size(right-
subtree) + 1
(d) Return tree_size
C++
// A recursive C++ program to
// calculate the size of the tree
#include <bits/stdc++.h>
usingnamespacestd;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
classnode
{
public:
intdata;
node* left;
node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(intdata)
{
node* Node = newnode();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
/* Computes the number of nodes in a tree. */
intsize(node* node)
{
if(node == NULL)
return0;
else
return(size(node->left) + 1 + size(node->right));
}
/* Driver code*/
intmain()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Size of the tree is "<< size(root);
return0;
}
// This code is contributed by rathbhupendra
C
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
structnode
{
intdata;
structnode* left;
structnode* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
structnode* newNode(intdata)
{
structnode* node = (structnode*)
malloc(sizeof(structnode));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Computes the number of nodes in a tree. */
intsize(structnode* node)
{
if(node==NULL)
return0;
else
return(size(node->left) + 1 + size(node->right));
}
/* Driver program to test size function*/
intmain()
{
structnode *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Size of the tree is %d", size(root));
getchar();
return0;
}
Java
// A recursive Java program to calculate the size of the tree
/* Class containing left and right child of current
node and key value*/
classNode
{
intdata;
Node left, right;
publicNode(intitem)
{
data = item;
left = right = null;
}
}
/* Class to find size of Binary Tree */
classBinaryTree
{
Node root;
/* Given a binary tree. Print its nodes in level order
using array for implementing queue */
intsize()
{
returnsize(root);
}
/* computes number of nodes in tree */
intsize(Node node)
{
if(node == null)
return0;
else
return(size(node.left) + 1+ size(node.right));
}
publicstaticvoidmain(String args[])
{
/* creating a binary tree and entering the nodes */
BinaryTree tree = newBinaryTree();
tree.root = newNode(1);
tree.root.left = newNode(2);
tree.root.right = newNode(3);
tree.root.left.left = newNode(4);
tree.root.left.right = newNode(5);
System.out.println("The size of binary tree is : "
+ tree.size());
}
}
Python3
# Python Program to find the size of binary tree
# A binary tree node
classNode:
# Constructor to create a new node
def__init__(self, data):
self.data =data
self.left =None
self.right =None
# Computes the number of nodes in tree
defsize(node):
ifnode isNone:
return0
else:
return(size(node.left)+1+size(node.right))
# Driver program to test above function
root =Node(1)
root.left =Node(2)
root.right =Node(3)
root.left.left =Node(4)
root.left.right =Node(5)
print("Size of the tree is %d"%(size(root)))
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
C#
usingSystem;
// A recursive C# program to calculate the size of the tree
/* Class containing left and right child of current
node and key value*/
publicclassNode
{
publicintdata;
publicNode left, right;
publicNode(intitem)
{
data = item;
left = right = null;
}
}
/* Class to find size of Binary Tree */
publicclassBinaryTree
{
publicNode root;
/* Given a binary tree. Print its nodes in level order
using array for implementing queue */
publicvirtualintsize()
{
returnsize(root);
}
/* computes number of nodes in tree */
publicvirtualintsize(Node node)
{
if(node == null)
{
return0;
}
else
{
return(size(node.left) + 1 + size(node.right));
}
}
publicstaticvoidMain(string[] args)
{
/* creating a binary tree and entering the nodes */
BinaryTree tree = newBinaryTree();
tree.root = newNode(1);
tree.root.left = newNode(2);
tree.root.right = newNode(3);
tree.root.left.left = newNode(4);
tree.root.left.right = newNode(5);
Console.WriteLine("The size of binary tree is : "+ tree.size());
}
}
// This code is contributed by Shrikant13
Javascript
<script>
// A recursive JavaScript program to
// calculate the size of the tree
/* Class containing left and right child of current
/* creating a binary tree and entering the nodes */
vartree = newBinaryTree();
tree.root = newNode(1);
tree.root.left = newNode(2);
tree.root.right = newNode(3);
tree.root.left.left = newNode(4);
tree.root.left.right = newNode(5);
document.write("Size of the tree is "+ tree.size() + "<br>");
</script>
Output:
Size of the tree is 5
Time Complexity: O(N)
As every node is visited once.
Auxiliary Space: O(N)
The extra space is due to the recursion call stack and the worst case occurs when the tree is either left skewed or right skewed.
Since this program is similar to traversal of tree, time and space complexities will be same as Tree traversal (Please see our Tree Traversal post for details)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy