Print all nodes that don't have sibling | Practice | GeeksforGeeks

Given a Binary Tree of size N, find all the nodes which don't have any sibling. You need to return a list of integers containing all the nodes that don't have a sibling in sorted order.
Note: Root node can not have a sibling so it cannot be included in our answer.
Example 1:
Input :
       37
      /   
    20
    /     
  113 

Output: 20 113
Explanation: 20 and 113 dont have any siblings.

Example 2:
Input :
       1
      / \
     2   3 

Output: -1
Explanation: Every node has a sibling.

Your Task:  
You dont need to read input or print anything. Complete the function noSibling() which takes the root of the tree as input parameter and returns a list of integers containing all the nodes that don't have a sibling in sorted order. If all nodes have a sibling, then the returning list should contain only one element -1.

Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(Height of the tree)

Constraints:
1 ≤ N ≤ 10^4
All nodes have distinct values.
0
utkarshagarwal10112 hours ago
void solve(Node* node, vector<int>&ans){
    if(!node)return;
    
    if(node->left&&node->right){
        solve(node->left,ans);
        solve(node->right,ans);
    }
    if(node->left&&!node->right){
        ans.push_back(node->left->data);
          solve(node->left,ans);
    }
    if(!node->left&&node->right){
        ans.push_back(node->right->data);
          solve(node->right,ans);
    }
}
vector<int> noSibling(Node* node)
{
    // code here
   vector<int>ans;
   solve(node,ans);
   if(ans.size()==0)
   {
       ans.push_back(-1);
       return ans;
   }
   else
   {    sort(ans.begin(),ans.end());
        return ans;
   }
  
}
0
keshavlad211 week ago
class Tree
{
    ArrayList<Integer> noSibling(Node root)
    {
       
         ArrayList<Integer>ans=new ArrayList<>();
         
          if( root==null || root.left== null && root.right==null){
             ans.add(-1);
             return ans;
         }
         noSib(root,ans);
         Collections.sort(ans);
         if(ans.size()==0)ans.add(-1);
         return ans;
    }
    public void noSib(Node root,ArrayList<Integer>a){
        if(root == null ){
            return;
        }
        if(root.left==null && root.right!=null){
            a.add(root.right.data);

        }
        if(root.right==null && root.left!=null){
            a.add(root.left.data);
        }
        noSib(root.left,a);
        noSib(root.right,a);
    }
}
0
satyamksingh021 week ago
class Tree
{
   ArrayList<Integer> noSibling(Node node)
   {
       // code here
       ArrayList<Integer> result=new ArrayList<>();
       if(node==null || (node.left==null && node.right==null)){
           result.add(-1);
           return result;
       }
       Queue<Node> q=new LinkedList<>();
       q.add(node);
       while(!q.isEmpty()){
           Node temp=q.poll();
           if(temp.left!=null){
               q.add(temp.left);
               if(temp.right==null){
                   result.add(temp.left.data);
               }
           }
           if(temp.right!=null){
               q.add(temp.right);
               if(temp.left==null){
                   result.add(temp.right.data);
               }
           }
       }
       
       Collections.sort(result);
       if(result.size()==0){
           result.add(-1);
       }
       return result;
   }
}
0
anshulgupta966262 weeks ago
void check(Node *root, vector<int> &ans){
    if(root->left == NULL && root->right != NULL){
        ans.push_back(root->right->data);
        check(root->right, ans);
    }
    if(root->left != NULL && root->right == NULL){
        ans.push_back(root->left->data);
        check(root->left, ans);
    }
    if(root->right == NULL && root->left == NULL){
        return;
    }
    if(root->left != NULL && root->right != NULL){
        check(root->left, ans);
        check(root->right, ans);
        return;
    }
}
vector<int> noSibling(Node* node)
{
    // code here
    vector<int> v1;
    check(node, v1);
    if(v1.empty()){
        v1.push_back(-1);
    }
    sort(v1.begin(), v1.end());
    return v1;
}
0
kumaarsahab4323 weeks ago
//Dont forget to sort the vector
void solve(Node* root, vector<int> &ans)
{
    if(root==NULL) return ;
    if(root->left==NULL and root->right!=NULL) ans.push_back(root->right->data) ;
    else if(root->right==NULL and root->left!=NULL) ans.push_back(root->left->data) ;
    solve(root->left,ans) ;
    solve(root->right,ans) ;
}
vector<int> noSibling(Node* node)
{
    vector<int> ans ;
    solve(node,ans) ;
    if(ans.size()==0) return {-1} ;
    sort(ans.begin(),ans.end()) ;
    return ans ;
}
0
navneet05082002kuswaha1 month ago
#python code
def solve(root,res):   if not root:       return   if root.right is None and root.left is not None:       res.append(root.left.data)   elif root.right is not None and root.left is None:       res.append(root.right.data)   solve(root.left,res)   solve(root.right,res)def noSibling(root):   res = []   solve(root,res)   if res:       res.sort()       return res   return [-1] 
0
roopsaisurampudi1 month ago
void noSibling(Node node, ArrayList<Integer> ans) {
        if (node == null) return;
        if (node.right == null && node.left != null) {
            ans.add(node.left.data);
        }
        else if (node.right != null && node.left == null) {
            ans.add(node.right.data);
        }
        noSibling(node.left, ans);
        noSibling(node.right, ans);
    }
    ArrayList<Integer> noSibling(Node node)
    {
        ArrayList<Integer> ans = new ArrayList<Integer>();
        noSibling(node, ans);
        if (ans.isEmpty()) ans.add(-1);
        Collections.sort(ans);
    
        return ans;
+1
sambitmishra1991 month ago
SIMPLE JAVA SOLUTION & EASY TO UNDERSTAND 
 
TIME COMPLEXITY: o(n)
SPACE COMPLEXITY: o(H)
 
where n is the no. of nodes in tree.
& H is the height of the tree.
H is equal to log(n) for a balanced tree.
 
 ArrayList<Integer> noSibling(Node node)   {       ArrayList<Integer> arr = new ArrayList<>();              noSibling(node , arr);              Collections.sort(arr);              if(arr.isEmpty()){           arr.add(-1);       }       return arr;   }         public void noSibling(Node root , ArrayList<Integer> arr){       if(root == null){           return ;       }               if(root.left!=null && root.right==null){           arr.add(root.left.data);       }       else if(root.left==null && root.right!=null){           arr.add(root.right.data);       }               noSibling(root.left , arr);        noSibling(root.right , arr);                  return ;   }
0
preranasingh20011 month ago
class Tree
{
   public ArrayList<Integer> list = new ArrayList<>();
   ArrayList<Integer> noSibling(Node root)
   {
   
      answer(root.left,root.right);
      if(list.size()>0){
          Collections.sort(list);
          return list;
      }
         list.add(-1);
         return list;
      
       // code here
   }
   public void answer(Node n1,Node n2){
       if(n1==null && n2==null){
           return;
       }
       
       if((n1!=null && n2==null) ||(n2!=null && n1==null)){
           int x=(n1==null)?n2.data:n1.data;
           list.add(x);
       }
       
       if(n1!=null){
       answer(n1.left,n1.right);
       }
       if(n2!=null){
       answer(n2.left,n2.right);
       }
       return;
   }
}
+1
akashpoojari1 month ago
Simple Java Solution
ArrayList<Integer> noSibling(Node node)
    {
        ArrayList<Integer> arr = new ArrayList<>();
        check(node , arr);
        Collections.sort(arr);
        if(arr.isEmpty()){
            arr.add(-1);
        }
        return arr;
    }
    public boolean check(Node root , ArrayList<Integer> arr){
        if(root == null){
            return false;
        }
        boolean left = check(root.left , arr);
        boolean right = check(root.right , arr);
        if(left && !right){
            arr.add(root.left.data);
        }else if(!left && right){
            arr.add(root.right.data);
        }
        return true;
    }
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.