Count Non-Leaf Nodes in Tree | Practice | GeeksforGeeks

Given a Binary Tree of size N, your task is to complete the function countNonLeafNodes(), that should return the count of all the non-leaf nodes of the given binary tree.
Example:
Input: 

Output:
2
Explanation:
Nodes 1 and 2 are the only non leaf nodes.

Your Task:
You don't need to take input or print anything. Your task is to complete the function countNonLeafNodes() that takes root as input and returns the number of non leaf nodes in the tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 ≤ Number of nodes ≤ 105
0
mayank20212 weeks ago
C++ : 0.07/1.07void helper(Node* root, int &count)   {       if(root && (root->left || root->right) )        {                 count++;                 helper(root->left, count);                 helper(root->right, count);                 }          }    int countNonLeafNodes(Node* root) {              int count=0;       helper(root, count);       return count;   }
0
harshscode2 weeks ago
   if(!root)       return 0;       if(!root->left and !root->right)       return 0;       return (1+countNonLeafNodes(root->left)+countNonLeafNodes(root->right));     
0
00thirt13n4 weeks ago
Easy C++ Solution:
int countNonLeafNodes(Node* root) {
    if(root==NULL)
    	return 0; 
    if(root->left==NULL && root->right==NULL))	
    	return 0;
    return (1+countNonLeafNodes(root->left)+countNonLeafNodes(root->right));
    }
+1
00thirt13n4 weeks ago
1-line C++ Solution:
int countNonLeafNodes(Node* root) {
        // Code here
        return (!root || (root->left==NULL && root->right==NULL))?0:(1+countNonLeafNodes(root->left)+countNonLeafNodes(root->right));
    }
0
jayendra05mishra1 month ago
class Solution { public:   int countNonLeafNodes(Node* root) {       int count=0;      // base case      if (root== NULL){          return 0;      }            if (root == NULL || (root->left == NULL && root->right == NULL)){     return 0;  }  return 1 + countNonLeafNodes(root->left) + countNonLeafNodes(root->right);         }};
0
imohdalam2 months ago
Java
class Solution
{
	int countNonLeafNodes(Node root) {
	    //code here
	    return sizeOfBT(root) - numberOfLeaves(root);
	}
	
	int sizeOfBT(Node root){
	    if(root == null)
	      return 0;
	    return sizeOfBT(root.left) + sizeOfBT(root.right) + 1;
	}
	
	int numberOfLeaves(Node root){
	    if(root == null)
	       return 0;
	       
	    if(root.left == null && root.right == null)
	       return 1;
	       
	   return numberOfLeaves(root.left) + numberOfLeaves(root.right);
	}
}
0
shankarmudiraj8272 months ago
class Solution{int countNonLeafNodes(Node root) {    if(root==null)    return 0;   if(root.left==null && root.right==null)       return 0;       if(root.left!=null || root.right!=null)       return countNonLeafNodes(root.left)+countNonLeafNodes(root.right)+1;   return -1;}}
0
maskiyashank883 months ago
Simple Java Solution
 
int countNonLeafNodes(Node node) {
	    //code here
	    if(node == null){
	        return 0;
	    }
	    if(node.left == null && node.right == null){
	        return 0;
	    }
	    return 1 + countNonLeafNodes(node.left) + countNonLeafNodes(node.right);
	}
0
amiransarimy3 months ago
Python Solutions
Total Time Taken:
0.2/1.3
 
class Solution:
   def countNonLeafNodes(self, root):
       
       if root is None:
           return 0
       
       if root.left is None and root.right is None:
           return 0
       
       return 1+ self.countNonLeafNodes(root.left) +self.countNonLeafNodes(root.right)
 
0
hydracody454 months ago
int countNonLeafNodes(Node* root) {       // Code here       if(root==NULL){           return 0;       }       if(root->left==NULL && root->right==NULL){           return 0;       }       return 1+countNonLeafNodes(root->right)+countNonLeafNodes(root->left);   }
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.