Size of Binary Tree | Practice | GeeksforGeeks

Given a binary tree of size N, you have to count number of nodes in it. For example, count of nodes in below tree is 4.
        1
     /      \
   10      39
  /
5
Input:
First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below: 


The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.


For example:

	For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N



The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.

The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.

For example:

	For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

For example:

	For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output:
For each testcase in new line, print the number of nodes.
User Task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function getSize().
Constraints:
1 <= T <= 30
1 <= N <= 104
Example:
Input:
2
1 2 3
10 5 9 N 1 3 6 
Output:
3
6
Explanation:
Testcase 2: Given Tree is :
                                10
                             /        \
                           5          9
                             \       /     \
                              1    3       6
There are six nodes in the tree .
 
0
shakshamkaushik11 day ago
class Tree{public static int getSize(Node root){    if(root == null){        return 0;    }    int left = getSize(root.left);    int right = getSize(root.right);            return left+right+1;   }} 
0
rahulvishwas26023 days ago
The easy way to get size of a Binary Tree
int getSize(Node* node){  // Your code here  if(node==NULL){      return 0;  }  return getSize(node->left)+getSize(node->right)+1;}
0
ershubhamkewat2 weeks ago
Java solution
 
 if(root==null){
           return 0;
       }else{
           return 1+getSize(root.left)+getSize(root.right);
       }
0
harshscode2 weeks ago
 if(root==NULL)  return 0;    if(root->left==NULL and root->right==NULL)  return 1;  return (1+getSize(root->left)+getSize(root->right));
0
mayank20212 weeks ago
C++ : 0.06/1.12void helper(Node* node, int &ans){   if(node)    {        ans++;        helper(node->left, ans);        helper(node->right, ans);    }   }
int getSize(Node* node){  int ans=0;  helper(node, ans);  return ans;}
+1
00thirt13n3 weeks ago
1-line C++:
int getSize(Node* node)
{
    return node==NULL?0:(1+getSize(node->left)+getSize(node->right));
}
0
00thirt13n3 weeks ago
Simple C++ Solution:
void anyorder(Node* root, int &size){
    if(!root) return;
    if(root) size++;
    anyorder(root->left, size);
    anyorder(root->right, size);
}
int getSize(Node* node)
{
   int size=0;
   anyorder(node, size);
   return size;
}
0
rajpateriya1 month ago
public static int getSize(Node root){    if(root == null)    {        return 0;    }    int count=0;    //keeping postordr trevrsal in mind;            count+=getSize(root.left);    count+= getSize(root.right);    count++;    return count;
   }
0
dsaconquered1 month ago
int getSize(Node* node){  // Your code here   if(node==nullptr)       return 0;   else{       return 1+getSize(node->left)+getSize(node->right);       }}
0
ilihaspatel441 month ago
/* simple C++ implementation vote me on top left small arrow bar if you like it */
int getSize(Node* node)
{
   if(node==NULL){
       return 0;
   }
   else{
       int count=0;
       count++;
       count+=getSize(node->left);
       count+=getSize(node->right);
       return count;
   }
   
   
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.