Print all nodes that don’t have sibling
Last Updated :
08 Mar, 2023
Given a Binary Tree, print all nodes that don’t have a sibling (a sibling is a node that has same parent. In a Binary Tree, there can be at most one sibling). Root should not be printed as root cannot have a sibling.
For example, the output should be “4 5 6” for the following tree.
This is a typical tree traversal question. We start from the root and check if the node has one child, if yes then print the only child of that node. If the node has both children, then recur for both the children.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
struct node *left, *right;
int key;
};
node* newNode( int key)
{
node *temp = new node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
void printSingles( struct node *root)
{
if (root == NULL)
return ;
if (root->left != NULL && root->right != NULL)
{
printSingles(root->left);
printSingles(root->right);
}
else if (root->right != NULL)
{
cout << root->right->key << " " ;
printSingles(root->right);
}
else if (root->left != NULL)
{
cout << root->left->key << " " ;
printSingles(root->left);
}
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->left->left = newNode(6);
printSingles(root);
return 0;
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree
{
Node root;
void printSingles(Node node)
{
if (node == null )
return ;
if (node.left != null && node.right != null )
{
printSingles(node.left);
printSingles(node.right);
}
else if (node.right != null )
{
System.out.print(node.right.data + " " );
printSingles(node.right);
}
else if (node.left != null )
{
System.out.print( node.left.data + " " );
printSingles(node.left);
}
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.right = new Node( 4 );
tree.root.right.left = new Node( 5 );
tree.root.right.left.right = new Node( 6 );
tree.printSingles(tree.root);
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def printSingles(root):
if root is None :
return
if root.left is not None and root.right is not None :
printSingles(root.left)
printSingles(root.right)
elif root.right is not None :
print (root.right.key,end = " " )
printSingles(root.right)
elif root.left is not None :
print (root.left.key,end = " " )
printSingles(root.left)
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.right = Node( 4 )
root.right.left = Node( 5 )
root.right.left.left = Node( 6 )
printSingles(root)
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree
{
public Node root;
public virtual void printSingles(Node node)
{
if (node == null )
{
return ;
}
if (node.left != null && node.right != null )
{
printSingles(node.left);
printSingles(node.right);
}
else if (node.right != null )
{
Console.Write(node.right.data + " " );
printSingles(node.right);
}
else if (node.left != null )
{
Console.Write(node.left.data + " " );
printSingles(node.left);
}
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(4);
tree.root.right.left = new Node(5);
tree.root.right.left.right = new Node(6);
tree.printSingles(tree.root);
}
}
|
Javascript
<script>
class Node {
constructor(item) {
this .data = item;
this .left = null ;
this .right = null ;
}
}
class BinaryTree {
constructor() {
this .root = null ;
}
printSingles(node) {
if (node == null ) {
return ;
}
if (node.left != null && node.right != null ) {
this .printSingles(node.left);
this .printSingles(node.right);
}
else if (node.right != null ) {
document.write(node.right.data + " " );
this .printSingles(node.right);
}
else if (node.left != null ) {
document.write(node.left.data + " " );
this .printSingles(node.left);
}
}
}
var tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(4);
tree.root.right.left = new Node(5);
tree.root.right.left.right = new Node(6);
tree.printSingles(tree.root);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(N) ,here N is number of nodes.
Alternate implementation in iterative method:
We start from the root and check if the node has one child, if yes then print the only child of that node. If the node has both children, then push both the children in the queue.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
struct node
{
struct node *left, *right;
int data;
};
node* newNode( int key)
{
node *temp = new node;
temp->data= key;
temp->left = temp->right = NULL;
return temp;
}
void printSingles( struct node *root)
{
if (root == NULL)
return ;
queue<node *> q1;
q1.push(root);
int flag=0;
vector< int > v;
while (q1.empty() == false )
{
struct node * temp=q1.front();
q1.pop();
if (temp->left != NULL &&
temp->right == NULL)
{
flag=1;
v.push_back(temp->left->data);
}
if (temp->left == NULL &&
temp->right != NULL)
{
flag=1;
v.push_back(temp->right->data);
}
if (temp->left != NULL)
{
q1.push(temp->left);
}
if (temp->right != NULL)
{
q1.push(temp->right);
}
}
sort(v.begin(), v.end());
for ( int i = 0; i < v.size(); i++)
{
cout<< v[i] << " " ;
}
if (v.size() == 0)
{
cout<< "-1" ;
}
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->left->left = newNode(6);
printSingles(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class node
{
node left, right;
int data;
};
static node newNode( int key)
{
node temp = new node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
static void printSingles(node root)
{
if (root == null )
return ;
Queue<node > q1 = new LinkedList<>();
q1.add(root);
int flag = 0 ;
Vector<Integer> v = new Vector<>();
while (q1.isEmpty() == false )
{
node temp = q1.peek();
q1.remove();
if (temp.left != null &&
temp.right == null )
{
flag = 1 ;
v.add(temp.left.data);
}
if (temp.left == null &&
temp.right != null )
{
flag = 1 ;
v.add(temp.right.data);
}
if (temp.left != null )
{
q1.add(temp.left);
}
if (temp.right != null )
{
q1.add(temp.right);
}
}
Collections.sort(v);
for ( int i = 0 ; i < v.size(); i++)
{
System.out.print( v.get(i) + " " );
}
if (v.size() == 0 )
{
System.out.print( "-1" );
}
}
public static void main(String[] args)
{
node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.right = newNode( 4 );
root.right.left = newNode( 5 );
root.right.left.left = newNode( 6 );
printSingles(root);
}
}
|
Python3
from queue import Queue
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def printSingles(root):
if root is None :
return
q1 = Queue(maxsize = 100 )
q1.put(root)
flag = 0
v = []
while q1.empty() = = False :
temp = q1.get()
if temp.left is not None and temp.right is None :
flag = 1
v.append(temp.left.key)
if temp.left is None and temp.right is not None :
flag = 1
v.append(temp.right.key)
if temp.left is not None :
q1.put(temp.left)
if temp.right is not None :
q1.put(temp.right)
v.sort()
for i in v:
print (i, end = " " )
if len (v) = = 0 :
print ( "-1" )
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.right = Node( 4 )
root.right.left = Node( 5 )
root.right.left.left = Node( 6 )
printSingles(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public
class node
{
public
node left, right;
public
int data;
};
static node newNode( int key)
{
node temp = new node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
static void printSingles(node root)
{
if (root == null )
return ;
Queue<node > q1 = new Queue<node>();
q1.Enqueue(root);
int flag = 0;
List< int > v = new List< int >();
while (q1.Count != 0)
{
node temp = q1.Peek();
q1.Dequeue();
if (temp.left != null &&
temp.right == null )
{
flag = 1;
v.Add(temp.left.data);
}
if (temp.left == null &&
temp.right != null )
{
flag = 1;
v.Add(temp.right.data);
}
if (temp.left != null )
{
q1.Enqueue(temp.left);
}
if (temp.right != null )
{
q1.Enqueue(temp.right);
}
}
v.Sort();
for ( int i = 0; i < v.Count; i++)
{
Console.Write( v[i] + " " );
}
if (v.Count == 0)
{
Console.Write( "-1" );
}
}
public static void Main(String[] args)
{
node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.right.left = newNode(5);
root.right.left.left = newNode(6);
printSingles(root);
}
}
|
Javascript
<script>
class node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function newNode(key) {
var temp = new node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
function printSingles( root) {
if (root == null )
return ;
var q1 = [];
q1.push(root);
var flag = 0;
var v = [];
while (q1.length != 0) {
var temp = q1.pop();
if (temp.left != null && temp.right == null ) {
flag = 1;
v.push(temp.left.data);
}
if (temp.left == null && temp.right != null ) {
flag = 1;
v.push(temp.right.data);
}
if (temp.left != null ) {
q1.push(temp.left);
}
if (temp.right != null ) {
q1.push(temp.right);
}
}
v.sort((a,b)=>a-b);
for (i = 0; i < v.length; i++) {
document.write(v[i] + " " );
}
if (v.length == 0) {
document.write( "-1" );
}
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.right.left = newNode(5);
root.right.left.left = newNode(6);
printSingles(root);
</script>
|
Time Complexity: O(N*log(N)), as we are visiting all node once.
Auxiliary Space: O(N) ,here N is number of nodes.
Share your thoughts in the comments
Please Login to comment...