Posted on by Kalkicode
Code Recursion

# Print the nodes of binary tree having a grandchild

The problem is to print the nodes of a binary tree that have at least one grandchild.

## Problem Statement

Given a binary tree, the task is to print the nodes of the tree that have at least one grandchild.

## Example Scenario

Consider the binary tree shown below:

``````       34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1``````

For this tree, the nodes with at least one grandchild are 34, -4, and 12.

## Idea to Solve the Problem

We can solve this problem using a recursive approach where we traverse the tree in a pre-order fashion. For each node, we check if it has at least one grandchild (i.e., either left-grandchild or right-grandchild). If it has, we print the node's value.

## Pseudocode

``````void printNode(struct TreeNode *node)
{
if (node != NULL)
{
if ((node->left != NULL && (node->left->left != NULL || node->left->right != NULL)) ||
(node->right != NULL && (node->right->right != NULL || node->right->left != NULL)))
{
printf(" %d", node->data); // Print grandchild node
}
printNode(node->left); // Visit left subtree
printNode(node->right); // Visit right subtree
}
}``````

## Algorithm Explanation

1. Implement a function `printNode` that takes a `TreeNode` pointer as input.
2. In the function, check if the current node has at least one grandchild by evaluating the conditions:
• If the left child exists and has at least one grandchild OR
• If the right child exists and has at least one grandchild
3. If the conditions are met, print the value of the current node.
4. Recursively call the `printNode` function for the left and right subtrees.
5. This will traverse the entire binary tree and print the nodes with at least one grandchild.
``````// C program for
// Print the nodes of binary tree having a grandchild
#include <stdio.h>
#include <stdlib.h>
// Tree Node
struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
// Create a dynamic tree
struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
if (tree != NULL)
{
tree->root = NULL;
}
else
{
printf("Memory Overflow to Create tree Tree\n");
}
//return new tree
return tree;
}
// This is creates and returns the new binary tree node
struct TreeNode *getNode(int data)
{
// Create dynamic node
struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
if (node != NULL)
{
//Set data and pointer values
node->data = data;
node->left = NULL;
node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return node;
}
//  Display node which having grandchild
void printNode(struct TreeNode *node)
{
if (node != NULL)
{

if((node->left != NULL &&
(node->left->left != NULL || node->left->right!=NULL))
||
(node->right != NULL &&
(node->right->right != NULL || node->right->left != NULL)))
{
// Print grandchild node
printf(" %d",node->data);
}
// Visit left subtree
printNode(node->left);
// Visit right subtree
printNode(node->right);

}
}

int main(int argc, char const *argv[])
{
struct BinaryTree *tree = newTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree->root = getNode(34);
tree->root->left = getNode(-4);
tree->root->left->right = getNode(12);
tree->root->left->right->left = getNode(6);
tree->root->left->right->left->left = getNode(9);
tree->root->left->right->left->right = getNode(1);
tree->root->left->right->right = getNode(8);
tree->root->left->left = getNode(7);
tree->root->left->left->left = getNode(4);
tree->root->right = getNode(10);
tree->root->right->right = getNode(6);

printNode(tree->root);
return 0;
}``````

#### input

`` 34 -4 12``
``````/*
Java Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
// Set initial tree root to null
this.root = null;
}
//  Display node which having grandchild
public void printNode(TreeNode node)
{
if (node != null)
{
if ((node.left != null
&& (node.left.left != null || node.left.right != null))
||
(node.right != null
&& (node.right.right != null || node.right.left != null)))
{
// Print grandchild node
System.out.print(" " + node.data );
}
// Visit left subtree
printNode(node.left);
// Visit right subtree
printNode(node.right);
}
}
public static void main(String[] args)
{
// Create new binary trees
BinaryTree tree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = new TreeNode(34);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(12);
tree.root.left.right.left = new TreeNode(6);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(1);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(7);
tree.root.left.left.left = new TreeNode(4);
tree.root.right = new TreeNode(10);
tree.root.right.right = new TreeNode(6);
tree.printNode(tree.root);
}
}``````

#### input

`` 34 -4 12``
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Find parent of leaf nodes in binary tree
*/

// Binary Tree node
class TreeNode
{
public:
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public: TreeNode *root;
BinaryTree()
{
this->root = NULL;
}
//  Display node which having grandchild
void printNode(TreeNode *node)
{
if (node != NULL)
{
if ((node->left != NULL
&&
(node->left->left != NULL || node->left->right != NULL))
||
(node->right != NULL
&& (node->right->right != NULL || node->right->left != NULL)))
{
// Print grandchild node
cout << " " << node->data;
}
// Visit left subtree
this->printNode(node->left);
// Visit right subtree
this->printNode(node->right);
}
}
};
int main()
{
// Create new binary trees
BinaryTree *tree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree->root = new TreeNode(34);
tree->root->left = new TreeNode(-4);
tree->root->left->right = new TreeNode(12);
tree->root->left->right->left = new TreeNode(6);
tree->root->left->right->left->left = new TreeNode(9);
tree->root->left->right->left->right = new TreeNode(1);
tree->root->left->right->right = new TreeNode(8);
tree->root->left->left = new TreeNode(7);
tree->root->left->left->left = new TreeNode(4);
tree->root->right = new TreeNode(10);
tree->root->right->right = new TreeNode(6);
tree->printNode(tree->root);
return 0;
}``````

#### input

`` 34 -4 12``
``````// Include namespace system
using System;
/*
Csharp Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
public class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
// Set initial tree root to null
this.root = null;
}
//  Display node which having grandchild
public void printNode(TreeNode node)
{
if (node != null)
{
if ((node.left != null
&& (node.left.left != null || node.left.right != null))
||
(node.right != null
&& (node.right.right != null || node.right.left != null)))
{
// Print grandchild node
Console.Write(" " + node.data);
}
// Visit left subtree
this.printNode(node.left);
// Visit right subtree
this.printNode(node.right);
}
}
public static void Main(String[] args)
{
// Create new binary trees
BinaryTree tree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = new TreeNode(34);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(12);
tree.root.left.right.left = new TreeNode(6);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(1);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(7);
tree.root.left.left.left = new TreeNode(4);
tree.root.right = new TreeNode(10);
tree.root.right.right = new TreeNode(6);
tree.printNode(tree.root);
}
}``````

#### input

`` 34 -4 12``
``````<?php
/*
Php Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
public \$data;
public \$left;
public \$right;
public	function __construct(\$data)
{
// Set node value
\$this->data = \$data;
\$this->left = NULL;
\$this->right = NULL;
}
}
class BinaryTree
{
public \$root;
public	function __construct()
{
\$this->root = NULL;
}
//  Display node which having grandchild
public	function printNode(\$node)
{
if (\$node != NULL)
{
if ((\$node->left != NULL
&& (\$node->left->left != NULL || \$node->left->right != NULL))
||
(\$node->right != NULL
&& (\$node->right->right != NULL || \$node->right->left != NULL)))
{
// Print grandchild node
echo(" ".\$node->data);
}
// Visit left subtree
\$this->printNode(\$node->left);
// Visit right subtree
\$this->printNode(\$node->right);
}
}
}

function main()
{
// Create new binary trees
\$tree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
\$tree->root = new TreeNode(34);
\$tree->root->left = new TreeNode(-4);
\$tree->root->left->right = new TreeNode(12);
\$tree->root->left->right->left = new TreeNode(6);
\$tree->root->left->right->left->left = new TreeNode(9);
\$tree->root->left->right->left->right = new TreeNode(1);
\$tree->root->left->right->right = new TreeNode(8);
\$tree->root->left->left = new TreeNode(7);
\$tree->root->left->left->left = new TreeNode(4);
\$tree->root->right = new TreeNode(10);
\$tree->root->right->right = new TreeNode(6);
\$tree->printNode(\$tree->root);
}
main();``````

#### input

`` 34 -4 12``
``````/*
Node JS Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
this.root = null;
}
//  Display node which having grandchild
printNode(node)
{
if (node != null)
{
if ((node.left != null
&& (node.left.left != null || node.left.right != null))
|| (node.right != null
&& (node.right.right != null || node.right.left != null)))
{
// Print grandchild node
process.stdout.write(" " + node.data);
}
// Visit left subtree
this.printNode(node.left);
// Visit right subtree
this.printNode(node.right);
}
}
}

function main()
{
// Create new binary trees
var tree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = new TreeNode(34);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(12);
tree.root.left.right.left = new TreeNode(6);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(1);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(7);
tree.root.left.left.left = new TreeNode(4);
tree.root.right = new TreeNode(10);
tree.root.right.right = new TreeNode(6);
tree.printNode(tree.root);
}
main();``````

#### input

`` 34 -4 12``
``````#    Python 3 Program
#    Find parent of leaf nodes in binary tree

#  Binary Tree node
class TreeNode :
def __init__(self, data) :
#  Set node value
self.data = data
self.left = None
self.right = None

class BinaryTree :
def __init__(self) :
self.root = None

#   Display node which having grandchild
def printNode(self, node) :
if (node != None) :
if ((node.left != None and
(node.left.left != None or node.left.right != None)) or
(node.right != None and
(node.right.right != None or node.right.left != None))) :
#  Print grandchild node
print(" ", node.data, end = "")

#  Visit left subtree
self.printNode(node.left)
#  Visit right subtree
self.printNode(node.right)

def main() :
#  Create new binary trees
tree = BinaryTree()
#        34
#       /   \
#     -4     10
#     / \      \
#    7   12     6
#   /   / \
#  4   6   8
#     / \
#    9   1
# -----------------
#  Constructing binary tree
tree.root = TreeNode(34)
tree.root.left = TreeNode(-4)
tree.root.left.right = TreeNode(12)
tree.root.left.right.left = TreeNode(6)
tree.root.left.right.left.left = TreeNode(9)
tree.root.left.right.left.right = TreeNode(1)
tree.root.left.right.right = TreeNode(8)
tree.root.left.left = TreeNode(7)
tree.root.left.left.left = TreeNode(4)
tree.root.right = TreeNode(10)
tree.root.right.right = TreeNode(6)
tree.printNode(tree.root)

if __name__ == "__main__": main()``````

#### input

``  34  -4  12``
``````#    Ruby Program
#    Find parent of leaf nodes in binary tree

#  Binary Tree node
class TreeNode
# Define the accessor and reader of class TreeNode
attr_accessor :data, :left, :right
def initialize(data)
#  Set node value
self.data = data
self.left = nil
self.right = nil
end

end

class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_accessor :root
def initialize()
self.root = nil
end

#   Display node which having grandchild
def printNode(node)
if (node != nil)
if ((node.left != nil &&
(node.left.left != nil || node.left.right != nil)) ||
(node.right != nil &&
(node.right.right != nil || node.right.left != nil)))
#  Print grandchild node
print(" ", node.data)
end

#  Visit left subtree
self.printNode(node.left)
#  Visit right subtree
self.printNode(node.right)
end

end

end

def main()
#  Create new binary trees
tree = BinaryTree.new()
#        34
#       /   \
#     -4     10
#     / \      \
#    7   12     6
#   /   / \
#  4   6   8
#     / \
#    9   1
# -----------------
#  Constructing binary tree
tree.root = TreeNode.new(34)
tree.root.left = TreeNode.new(-4)
tree.root.left.right = TreeNode.new(12)
tree.root.left.right.left = TreeNode.new(6)
tree.root.left.right.left.left = TreeNode.new(9)
tree.root.left.right.left.right = TreeNode.new(1)
tree.root.left.right.right = TreeNode.new(8)
tree.root.left.left = TreeNode.new(7)
tree.root.left.left.left = TreeNode.new(4)
tree.root.right = TreeNode.new(10)
tree.root.right.right = TreeNode.new(6)
tree.printNode(tree.root)
end

main()``````

#### input

`` 34 -4 12``
``````/*
Scala Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode(var data: Int,
var left: TreeNode,
var right: TreeNode)
{
def this(data: Int)
{
// Set node value
this(data,null,null);
}
}
class BinaryTree(var root: TreeNode)
{
def this()
{
this(null);
}
//  Display node which having grandchild
def printNode(node: TreeNode): Unit = {
if (node != null)
{
if ((node.left != null
&& (node.left.left != null || node.left.right != null))
|| (node.right != null
&& (node.right.right != null || node.right.left != null)))
{
// Print grandchild node
print(" " + node.data);
}
// Visit left subtree
printNode(node.left);
// Visit right subtree
printNode(node.right);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create new binary trees
var tree: BinaryTree = new BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = new TreeNode(34);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(12);
tree.root.left.right.left = new TreeNode(6);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(1);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(7);
tree.root.left.left.left = new TreeNode(4);
tree.root.right = new TreeNode(10);
tree.root.right.right = new TreeNode(6);
tree.printNode(tree.root);
}
}``````

#### input

`` 34 -4 12``
``````/*
Swift 4 Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: TreeNode? ;
init()
{
self.root = nil;
}
//  Display node which having grandchild
func printNode(_ node: TreeNode? )
{
if (node  != nil)
{
if ((node!.left  != nil
&& (node!.left!.left  != nil || node!.left!.right  != nil))
|| (node!.right  != nil
&& (node!.right!.right  != nil || node!.right!.left  != nil)))
{
// Print grandchild node
print(" ", node!.data, terminator: "");
}
// Visit left subtree
self.printNode(node!.left);
// Visit right subtree
self.printNode(node!.right);
}
}
}
func main()
{
// Create new binary trees
let tree = BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = TreeNode(34);
tree.root!.left = TreeNode(-4);
tree.root!.left!.right = TreeNode(12);
tree.root!.left!.right!.left = TreeNode(6);
tree.root!.left!.right!.left!.left = TreeNode(9);
tree.root!.left!.right!.left!.right = TreeNode(1);
tree.root!.left!.right!.right = TreeNode(8);
tree.root!.left!.left = TreeNode(7);
tree.root!.left!.left!.left = TreeNode(4);
tree.root!.right = TreeNode(10);
tree.root!.right!.right = TreeNode(6);
tree.printNode(tree.root);
}
main();``````

#### input

``  34  -4  12``
``````/*
Kotlin Program
Find parent of leaf nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
var root: TreeNode ? ;
constructor()
{
this.root = null;
}
//  Display node which having grandchild
fun printNode(node: TreeNode ? ): Unit
{
if (node != null)
{
if ((node.left != null
&& (node.left?.left != null || node.left?.right != null))
|| (node.right != null
&& (node.right?.right != null || node.right?.left != null)))
{
// Print grandchild node
print(" " + node.data);
}
// Visit left subtree
this.printNode(node.left);
// Visit right subtree
this.printNode(node.right);
}
}
}
fun main(args: Array < String > ): Unit
{
// Create new binary trees
val tree: BinaryTree = BinaryTree();
/*
34
/   \
-4     10
/ \      \
7   12     6
/   / \
4   6   8
/ \
9   1
-----------------
Constructing binary tree
*/
tree.root = TreeNode(34);
tree.root?.left = TreeNode(-4);
tree.root?.left?.right = TreeNode(12);
tree.root?.left?.right?.left = TreeNode(6);
tree.root?.left?.right?.left?.left = TreeNode(9);
tree.root?.left?.right?.left?.right = TreeNode(1);
tree.root?.left?.right?.right = TreeNode(8);
tree.root?.left?.left = TreeNode(7);
tree.root?.left?.left?.left = TreeNode(4);
tree.root?.right = TreeNode(10);
tree.root?.right?.right = TreeNode(6);
tree.printNode(tree.root);
}``````

#### input

`` 34 -4 12``

## Output Explanation

The code implements the algorithm and prints the nodes of the binary tree that have at least one grandchild. It displays the node values in the order they are encountered during the traversal.

## Time Complexity

The time complexity of this algorithm is O(N), where N is the number of nodes in the binary tree. This is because we perform a single pre-order traversal through all nodes of the binary tree. The space complexity is O(H), where H is the height of the binary tree, due to the recursive call stack.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post