Full binary tree | Practice | GeeksforGeeks

Given a Binary Tree. Check whether the Binary tree is a full binary tree or not.
Example 1:
Input:
          1
       /    \
      2      3
    /   \
   4     5
Output: 1
Explanation: Every node except leaf node
has two children so it is a full tree.

Example 2:
Input:
          1
       /    \
      2      3
    /   
   4     
Output: 0
Explanation: Node 2 has only one child
so this is not a full tree.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isFullTree() which takes the root node of the tree as input and returns True if the given Binary Tree is full. Else, it returns False. (The driver code will print 1 if the returned value is true, otherwise 0).
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1<=Number of nodes<=1000
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
imaniket2 months ago
bool isFullTree (struct Node* root)
{
  if(!root) return false;
  if(!root->left && !root->right) return true;
  
  if(isFullTree(root->left) && isFullTree(root->right)) return true;
  else return false;
}
0
mayank20212 months ago
C++bool isFullTree (struct Node* root){
if( !root ||  (!root->left && !root->right) )return 1;else if((root->left && !root->right) || (!root->left && root->right))return 0;elsereturn isFullTree(root->left) && isFullTree(root->right);}
+1
feyza2 months ago
class GfG
{
	boolean isFullTree(Node node)
    {
        if(node == null) return true;
        
        if(node.left == null && node.right == null) return true;
        
        if(node.left != null && node.right != null)
        {
            return (isFullTree(node.right) && isFullTree(node.left));
        }    
        return false;
    }
}
0
hamidnourashraf2 months ago
def isFullTree(root):
    if root is None:
        return True
    if root.left is None and root.right is None:
        return True
    if root.left is None and root.right is not None:
        return False
    if root.left is not None and root.right is None:
        return False
    return (isFullTree(root.left) and isFullTree(root.right))
+1
shashikantsolanki0423 months ago
C++ Level order traversal Soln.
 
bool isFullTree (struct Node* root){
    queue<Node*>q;
    q.push(root);
    while(!q.empty()){
        Node* top = q.front();
        int ctr = 0;
        q.pop();
        if(top->left){
            ctr++;
            q.push(top->left);
        }
        if(top->right){
            ctr++;
            q.push(top->right);
        }
        if(ctr == 1){
            return false;
        }
    }
    return true;
}
 
0
bhushan5613 months ago
C++ Solution
 
bool isFullTree (struct Node* root){//add code here.if(root==NULL)return true;else if(root->left==NULL && root->right==NULL)return true;else  if(root->left!=NULL && root->right!=NULL)return isFullTree(root->left) && isFullTree(root->right);else if(root->left || root->right)return false;} 
0
ee19b0373 months ago
bool isFullTree (struct Node* root)
{
//add code here
    if(!root) return 1;
    if(root->left==NULL ^ root->right==NULL) return 0;
    return isFullTree(root->left) && isFullTree(root->right);
    
}
0
amanpandey30073 months ago
bool isFullTree (struct Node* root){  if(!root)  return true;    if(!root->left and !root->right)  return true;    if(!root->left or !root->right)  return false;      bool a=isFullTree(root->left);  bool b=isFullTree (root->right);    return a and b;  }
0
harishk2711994 months ago
void solve(Node* root , int & f){
    if(root==NULL) return;
    if((root->left and !root->right ) or (!root->left and root->right )) {
        f=0;
        return ;
        
    }
    if(root->left and root->right){
        solve(root->left,f);
        solve(root->right,f);
        
    }
}
bool isFullTree (struct Node* root)
{
//add code here.
    int f=1;
    solve(root,f);
    return f;
    
    
 }
0
anjalipatel94314 months ago
bool isFullTree (struct Node* root){//add code here.   if(root==NULL || (root->left==NULL && root->right==NULL) )  return 1;  if(root->left==NULL && root->right!=NULL)  return 0;     if(root->left!=NULL && root->right==NULL )  return 0;    return isFullTree(root->left) && isFullTree(root->right);}
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.