Foldable Binary Tree | Practice | GeeksforGeeks

Given a binary tree, check if the tree can be folded or not. A tree can be folded if left and right subtrees of the tree are structure wise same. An empty tree is considered as foldable.
Consider the below trees:
(a) and (b) can be folded.
(c) and (d) cannot be folded.
Example 1:
Input:
     10
    /    \
   7     15
 /  \   /  \
N   9  11   N
Output:Yes

Example 2:
Input:
      10
    /    \
   7     15
 /  \   /  \
5   N  11   N
Output: No


Your Task:
The task is to complete the function isFoldable() that takes root of the tree as input and returns true or false depending upon whether the tree is foldable or not.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
0 <= n <= 103
1 <= data of node <= 104
0
singhdipranjan673 days ago
//very simple solution
boolean IsFoldable(Node root) 
	{ 
	    if(root==null)
	    return true;
	    return DFS(root.left, root.right);
	} 

    boolean DFS(Node root1, Node root2)
    {
        if(root1==null && root2==null)
        return true;
        if((root1==null && root2!=null) || (root2==null && root1!=null))
        return false;
        return (DFS(root1.left, root2.right) && DFS(root1.right, root2.left));
    }
}
0
amankumar2783 weeks ago
Java Solution:
 
class Tree{     boolean check(Node root1,Node root2){       if(root1==null&&root2==null){        return true;    } if(root1==null||root2==null){        return false;    }      return check(root1.left,root2.right)&&check(root1.right,root2.left);   }     //Function to check whether a binary tree is foldable or not.   boolean IsFoldable(Node node) {     if(node==null){        return true;    } return check(node.left,node.right);} }
0
anujsonijnv171 month ago
//very simple and easy recursive c++ code
 
bool helper(Node* t,Node* s){   if(t==NULL&&s==NULL)return 1;   if(t==NULL||s==NULL)return 0;         return helper(t->left,s->right) && helper(t->right,s->left);}
bool IsFoldable(Node* root){ if (root==NULL)return true; return helper(root->left,root->right);}
0
gujjulassr1 month ago
0.2/1.4
 
   boolean IsFoldable(Node node) {  // your code  if(node==null){     return true; } Deque<Node> deq=new LinkedList<Node>(); Deque<Node> out=new LinkedList<Node>(); out.addLast(node); while(!out.isEmpty()){     int count=out.size();          for(int i=0;i<count;i++){         Node curr=out.removeFirst();         deq.addLast(curr.left);         if(curr.left!=null){             out.addLast(curr.left);         }         if(curr.right!=null){             out.addLast(curr.right);         }         deq.addLast(curr.right);     }
     if(!match(deq)){         return false;     }     deq.clear(); } return true;} public boolean match(Deque<Node> deq){    while(!deq.isEmpty()){        Node front=deq.removeFirst();        if(deq.isEmpty()){                        return true;        }        Node back=deq.removeLast();        if(back!=null && front==null){            return false;        }        if(back==null && front!=null){            return false;        }    }    return true;}}
0
forcer
This comment was deleted.
+1
ishrivastava252 months ago
//Easy Solution 
 
bool helper(Node* root1,Node* root2){   if(root1==NULL && root2==NULL) return true;   if(root1==NULL || root2==NULL) return false;   return helper(root1->left,root2->right) && helper(root1->right,root2->left);}
bool IsFoldable(Node* root){   if(root==NULL || (root->left==NULL && root->right==NULL))   {       return true;   }   if(root->left==NULL || root->right==NULL) return false;   return helper(root->left,root->right);}
0
krishnarathi3412 months ago
c++ solution
We have to simultaneously check for left and right sub-tree Nodes 
so, we would iterate over the tree using postorder and recusively check for right and left tree has foldable structure or not 
During, iteration if there arises a case where subtrees i.e. t1 (root→left) and t2 (root→right)   t1=NULL && t2≠NULL We would return false else true;
if(t1==NULL) return t1==t2;
The above condition checks for all possiblilites related to T1 is NULL or not and T2 is NULL or not.
bool checkLeftandRight(Node *t1,Node *t2){
    if(t1==NULL) return t1==t2;
    if(t1!=NULL && t2!=NULL){
        bool l = checkLeftandRight(t1->left,t2->right);
        bool r = checkLeftandRight(t1->right,t2->left);
        return (l && r);
    }
    return false;
}

//Function to check whether a binary tree is foldable or not.
bool IsFoldable(Node* root)
{
    // Your code goes here
     if(root==NULL) return true;
    return checkLeftandRight(root->left,root->right);
}

0
siddhant073 months ago
Iterative Solution using deque O(N) time and O(N) space
bool IsFoldable(Node* root)
{
    if(root == NULL){
        return true;
    }
    deque<Node*> dq;
    dq.push_back(root);
    while(dq.empty() == false){
       int count = dq.size();
       deque<Node*> d;
       for(int i = 0; i < count; i++){
           Node* f = dq[i];
           Node* b = dq[count - i - 1];
           if(f->left != NULL){
               if(b->right != NULL){
                   d.push_back(f->left);
               }
               else{
                   return false;
               }
           }
           if(f->right != NULL){
               if(b->left != NULL){
                   d.push_back(f->right);
               }
               else{
                   return false;
               }
           }
       }
       dq = d;
    }
    return true;
}
 
0
vishnu12653 months ago
Java Solution :
class Tree
{
    //Function to check whether a binary tree is foldable or not.
    boolean IsFoldable(Node node) 
	{ 
	    if(node==null) return true;
		return foldable(node.left,node.right);
	} 
	
	boolean foldable(Node root1,Node root2){
	    if(root1==null && root2==null) return  true;
	    else if(root1==null || root2==null) return false;
	    else return foldable(root1.left,root2.right) && foldable(root1.right,root2.left);
	}
	
	
}
+1
kronizerdeltac3 months ago
JAVA SOLUTION 
 
boolean IsFoldable_(Node root1, Node root2) {              if(root1 == null && root2 == null)           return true;                  if(root1 == null || root2 == null)           return false;              return IsFoldable_(root1.left, root2.right) && IsFoldable_(root1.right, root2.left);
   }   boolean IsFoldable(Node root) {  if(root == null)     return true;       return IsFoldable_(root, root);}
   }   boolean IsFoldable(Node root) {  if(root == null)     return true;       return IsFoldable_(root, root);}
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.