Print the nodes having exactly one child in a Binary tree
Given a binary tree, the task is to print all the nodes having exactly one child. Print “-1” if no such node exists.
Examples:
Input:
2
/ \
3 5
/ / \
7 8 6
Output: 3
Explanation:
There is only one node having
single child that is 3.
Input:
9
/ \
7 8
/ \
4 3
Output: -1
Explanation:
There is no node having exactly one
child in the binary tree.
Approach: The idea is to traverse the tree in the inorder traversal and at each step of the traversal check that if the node is having exactly one child. Then append that node into a result array to keep track of such nodes. After the traversal, simply print each element of this result array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
Node( int x)
{
data = x;
left = right = NULL;
}
};
vector< int > lst;
void printNodesOneChild(Node* root)
{
if (root == NULL)
return ;
if (root->left != NULL &&
root->right == NULL ||
root->left == NULL &&
root->right != NULL)
{
lst.push_back(root->data);
}
printNodesOneChild(root -> left);
printNodesOneChild(root -> right);
}
int main()
{
Node *root = new Node(2);
root -> left = new Node(3);
root -> right = new Node(5);
root -> left -> left = new Node(7);
root -> right -> left = new Node(8);
root -> right -> right = new Node(6);
printNodesOneChild(root);
if (lst.size() == 0)
printf ( "-1" );
else
{
for ( int value : lst)
{
cout << (value) << endl;
}
}
}
|
Java
import java.util.Vector;
class Node
{
int data;
Node left, right;
Node( int data)
{
this .data = data;
}
}
class GFG{
static Vector<Integer> list = new Vector<>();
static void printNodesOneChild(Node root)
{
if (root == null )
return ;
if (root.left != null && root.right == null ||
root.left == null && root.right != null )
{
list.add(root.data);
}
printNodesOneChild(root.left);
printNodesOneChild(root.right);
}
public static void main(String[] args)
{
Node root = new Node( 2 );
root.left = new Node( 3 );
root.right = new Node( 5 );
root.left.left = new Node( 7 );
root.right.left = new Node( 8 );
root.right.right = new Node( 6 );
printNodesOneChild(root);
if (list.size() == 0 )
System.out.println(- 1 );
else
{
for ( int value : list)
{
System.out.println(value);
}
}
}
}
|
Python3
class node:
def __init__( self , val):
self .val = val
self .left = None
self .right = None
single_child_nodes = []
def printNodesOneChild(root):
if not root:
return
if not root.left and root.right:
single_child_nodes.append(root)
elif root.left and not root.right:
single_child_nodes.append(root)
printNodesOneChild(root.left)
printNodesOneChild(root.right)
return
if __name__ = = "__main__" :
root = node( 2 )
root.left = node( 3 )
root.right = node( 5 )
root.left.left = node( 7 )
root.right.left = node( 8 )
root.right.right = node( 6 )
printNodesOneChild(root)
if not len (single_child_nodes):
print ( - 1 )
else :
for i in single_child_nodes:
print (i.val, end = " " )
print ()
|
C#
using System;
using System.Collections;
class GFG{
public class Node
{
public Node left;
public Node right;
public int data;
};
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static ArrayList list = new ArrayList();
static void printNodesOneChild(Node root)
{
if (root == null )
return ;
if (root.left != null && root.right == null ||
root.left == null && root.right != null )
{
list.Add(root.data);
}
printNodesOneChild(root.left);
printNodesOneChild(root.right);
}
static public void Main()
{
Node root = newNode(2);
root.left = newNode(3);
root.right = newNode(5);
root.left.left = newNode(7);
root.right.left = newNode(8);
root.right.right = newNode(6);
printNodesOneChild(root);
if (list.Count == 0)
Console.WriteLine(-1);
else
{
foreach ( int value in list)
{
Console.WriteLine(value);
}
}
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left= this .right= null ;
}
}
let list = [];
function printNodesOneChild(root)
{
if (root == null )
return ;
if (root.left != null && root.right == null ||
root.left == null && root.right != null )
{
list.push(root.data);
}
printNodesOneChild(root.left);
printNodesOneChild(root.right);
}
let root = new Node(2);
root.left = new Node(3);
root.right = new Node(5);
root.left.left = new Node(7);
root.right.left = new Node(8);
root.right.right = new Node(6);
printNodesOneChild(root);
if (list.length == 0)
document.write(-1);
else
{
document.write(list.join( "<br>" ))
}
</script>
|
Time complexity: O(n) where n is no of nodes in binary tree
Auxiliary Space: O(n)
Last Updated :
16 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...