The idea is to recursively calculate the size of tree. For each node (starting from root node), calculate the size of left subtree and right subtree and return the size of current subtree (size of left subtree + size of right subtree + 1).
C++
// C++ program to find the size// of a binary tree.#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Recursive function to find the // size of binary tree.intgetSize(Node*root){if(root==nullptr)return0;// Find the size of left and right // subtree.intleft=getSize(root->left);intright=getSize(root->right);// return the size of curr subtree.returnleft+right+1;}intmain(){// Constructed binary tree is// 1// / \ // 2 3// / \ // 4 5Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);cout<<getSize(root)<<endl;return0;}
C
// C program to find the size// of a binary tree.#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Recursive function to find the // size of binary tree.intgetSize(structNode*root){if(root==NULL)return0;// Find the size of left and right // subtree.intleft=getSize(root->left);intright=getSize(root->right);// return the size of curr subtree.returnleft+right+1;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// Constructed binary tree is// 1// / \ // 2 3// / \ // 4 5structNode*root=createNode(1);root->left=createNode(2);root->right=createNode(3);root->left->left=createNode(4);root->left->right=createNode(5);printf("%d\n",getSize(root));return0;}
Java
// Java program to find the size// of a binary tree.importjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Recursive function to find the // size of binary tree.staticintgetSize(Noderoot){if(root==null)return0;// Find the size of left and right // subtree.intleft=getSize(root.left);intright=getSize(root.right);// return the size of curr subtree.returnleft+right+1;}publicstaticvoidmain(String[]args){// Constructed binary tree is// 1// / \// 2 3// / \// 4 5Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);System.out.println(getSize(root));}}
Python
# Python program to find the size# of a binary tree.classNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Recursive function to find the # size of binary tree.defgetSize(root):ifrootisNone:return0# Find the size of left and right # subtree.left=getSize(root.left)right=getSize(root.right)# return the size of curr subtree.returnleft+right+1if__name__=="__main__":# Constructed binary tree is# 1# / \# 2 3# / \# 4 5root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)print(getSize(root))
C#
// C# program to find the size// of a binary tree.usingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Recursive function to find the // size of binary tree.staticintGetSize(Noderoot){if(root==null)return0;// Find the size of left and right // subtree.intleft=GetSize(root.left);intright=GetSize(root.right);// return the size of curr subtree.returnleft+right+1;}staticvoidMain(string[]args){// Constructed binary tree is// 1// / \// 2 3// / \// 4 5Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);Console.WriteLine(GetSize(root));}}
JavaScript
// JavaScript program to find the size// of a binary tree.classNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Recursive function to find the // size of binary tree.functiongetSize(root){if(root===null)return0;// Find the size of left and right // subtree.letleft=getSize(root.left);letright=getSize(root.right);// return the size of curr subtree.returnleft+right+1;}// Constructed binary tree is// 1// / \// 2 3// / \// 4 5letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);console.log(getSize(root));
Output
5
Time Complexity: O(n), where n is the number of nodes in binary tree. Auxiliary Space: O(h), where h is the height of the tree.