Check whether BST contains Dead End | Practice | GeeksforGeeks

Given a Binary search Tree that contains positive integer values greater then 0. The task is to complete the function isDeadEnd which returns true if the BST contains a dead end else returns false. Here Dead End means, we are not able to insert any element after that node.
Examples:
Input :   
               8
             /   \ 
           5      9
         /  \     
        2    7 
       /
      1     
          
Output : Yes
Explanation : Node "1" is the dead End because after that 
              we cant insert any element.       


Input :     
              8
            /   \ 
           7     10
         /      /   \
        2      9     13

Output : Yes
Explanation : We can't insert any element at 
              node 9.  

Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the no of nodes of the BST . Second line of each test case consists of 'N' space separated integers denoting the elements of the BST. These elements are inserted into BST in the given order.

Output:
The output for each test case will be 1 if the BST contains a dead end else 0.
 
Constraints:
1<=T<=100
1<=N<=200
 
Example(To be used only for expected output):
Input:
2
6
8 5 9 7 2 1
6
8 7 10 9 13 2
Output:
1
1
0
parasdhanwal1 week ago
<<Java Solution>>
 
class GFG
{
    public static boolean isDeadEnd(Node root)
    {
        return solve(root,1, Integer.MAX_VALUE);
    }
    
    static boolean solve(Node root, int min, int max){
        if(root==null)
        return false;
        if(root.left==null && root.right==null)
        {
            if(min==max){
                return true;
            }
                
            else 
                return false;
            }
            
            return solve(root.left,min,root.data-1)||solve(root.right,root.data+1,max);
        }
        
        
    }
+3
neerajchatterjee23014 weeks ago
bool solve(Node* root, int mn, int mx){
   
   if(root == NULL) return 0;
   if(root->data == mn and root->data == mx) return 1;
   
   return solve(root->left, mn, root->data - 1) or solve(root->right, root->data+1, mx);
   
}
bool isDeadEnd(Node *root)
{
   int mn = 1, mx = INT_MAX;
   return solve(root, mn, mx);
}
0
amarrajsmart1971 month ago
bool solve(Node* root,int l,int h){   if(!root)   {       return false;   }   if(l==h)   return true;   bool a=solve(root->left,l,root->data-1);   bool b=solve(root->right,root->data+1,h);   return a||b;   }bool isDeadEnd(Node *root){   //Your code here   return solve(root,1,INT_MAX);}
0
ruchitchudasama1231 month ago
// can't make much smaller then this.

bool solve(Node* root,int x,int inf){
    if(!root)return false;
    if(x==inf)return true;
    return (solve(root->left,x,root->data-1) || solve(root->right,root->data+1,inf));
}
bool isDeadEnd(Node *root)
{
    if(!root)return false;
    return solve(root,1,INT_MAX);
}
0
ruchitchudasama1231 month ago
bool isLeaf(Node* root){
    if(root->left==NULL && root->right==NULL){
        return true;
    }
    return false;
}

void solve(Node* root,unordered_set<int> &s,unordered_set<int> &s2){
    if(root){
        solve(root->left,s,s2);
        solve(root->right,s,s2);
        s.insert(root->data);
        if(isLeaf(root)){
            s2.insert(root->data);
        }
    }
}
bool isDeadEnd(Node *root)
{
    unordered_set<int> s;
    unordered_set<int> s2;
    solve(root,s,s2);
    for(auto it:s2){
        if((s.find(it+1)!=s.end() &&  s.find(it-1)!=s.end()) || (it==1 && s.find(it+1)!=s.end())){
            return true;
        }
    }
    return false;
}
+2
shilsoumyadip1 month ago
bool solve(Node *root,int r1,int r2){   if(!root) return false;   if(r1==r2) return true;   bool a = solve(root->left,r1,root->data-1);   bool b = solve(root->right,root->data+1,r2);   return  a or b;}bool isDeadEnd(Node *root){   //Your code here   bool a = solve(root,1,INT_MAX);   return a;}
0
aditirai630632 months ago
Java: (min and max value of range coincided it means that we cannot add any node)
 
 public static boolean isDeadEnd(Node root){       return deadEnd(root,1,Integer.MAX_VALUE);}     static public boolean deadEnd(Node root,Integer min,Integer max){          if (root==null)       return false;   if (min == max)       return true;   return deadEnd(root.left, min, root.data - 1)||               deadEnd(root.right, root.data + 1, max);
+2
forcer2 months ago
bool solve(Node *root,int min,int max)
{
    if(!root) return false;
    if(min==max) return true;
    return solve(root->left,min,root->data-1)||solve(root->right,root->data+1,max);
}
bool isDeadEnd(Node *root)
{
    //Your code here
    return solve(root,1,INT_MAX);
}
0
sangamchoudhary72 months ago
bool solve(Node* root,int lb,int ub){
    if(!root) return false;
    else if(root->data - lb == 1 and ub - root->data == 1) return true;
    
    bool left_check = solve(root->left,lb,root->data);
    bool right_check = solve(root->right,root->data,ub);
    
    bool res = left_check or right_check;
    return res;
}

bool isDeadEnd(Node *root){
    return solve(root,0,INT_MAX);
}
+1
golaravi5552 months ago
void inorder(Node* root,vector<int>&v,unordered_set<int>&s){   if(root){   inorder(root->left,v,s);   if(!root->left&&!root->right)v.push_back(root->data);   s.insert(root->data);   inorder(root->right,v,s);   }}
bool isDeadEnd(Node *root){   //Your code here   unordered_set<int>s;   vector<int>v;   inorder(root,v,s);   for(int i=0;i<v.size();i++){       if(v[i]-1<=0){            if(s.find(v[i]+1)!=s.end())return true;       }       else{           if(s.find(v[i]-1)!=s.end()&&s.find(v[i]+1)!=s.end())return true;       }   }   return false;}
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.