Print the all leaf nodes in N ary tree
This article plunges into the intriguing realm of N-ary trees, unveiling the process of printing all the leaf nodes present within such trees. Through a detailed breakdown of the problem, the strategy to solve it, and the code implementation, readers will gain a comprehensive understanding of how to navigate and identify leaf nodes within N-ary trees.
Problem Statement
Given an N-ary tree, the task is to print all the leaf nodes within the tree.
Example
Consider the following N-ary tree:
10
/ \
/ \
/ \
8 5
/|\ /|\
/ | \ / | \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
The output is:
-2 9 17 12 6 7 18 3 2 1 3
Solution Strategy
- Create a class
TreeNode
to represent nodes in the N-ary tree. This class should contain a value and a vector of children. - Implement the
NAryTree
class, responsible for building the N-ary tree and printing the leaf nodes. - Traverse the tree and print the value of nodes that have no children, i.e., leaf nodes.
Code Solution
import java.util.Vector;
import java.util.ArrayList;
// Java program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
public int key;
public Vector < TreeNode > child;
public TreeNode(int key)
{
this.key = key;
this.child = new Vector < TreeNode > ();
}
public void addChild(int key)
{
TreeNode t = new TreeNode(key);
this.child.add(t);
}
}
public class NAryTree
{
public TreeNode root;
public NAryTree()
{
// Set initial tree root to null
this.root = null;
}
// Print all leaf nodes
public void printLeafNode(TreeNode node)
{
if (node == null)
{
return;
}
int i = 0;
// iterating the child of given node
while (i < node.child.size())
{
// Recursively visit child node
printLeafNode(node.child.get(i));
i++;
}
if (node.child.size() == 0)
{
// When node is left node
System.out.print(" " + node.key);
}
}
public static void main(String[] args)
{
NAryTree tree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = new TreeNode(10);
tree.root.addChild(8);
tree.root.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root.child.get(0).addChild(-2);
tree.root.child.get(0).addChild(1);
tree.root.child.get(0).addChild(6);
// Add child node [9,11] in node (1)
tree.root.child.get(0).child.get(1).addChild(9);
tree.root.child.get(0).child.get(1).addChild(11);
// Add child node [17 12] in node (11)
tree.root.child.get(0).child.get(1).child.get(1).addChild(17);
tree.root.child.get(0).child.get(1).child.get(1).addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root.child.get(1).addChild(7);
tree.root.child.get(1).addChild(18);
tree.root.child.get(1).addChild(3);
tree.root.child.get(1).addChild(4);
// Add child node [2,1,3] in node (7)
tree.root.child.get(1).child.get(3).addChild(2);
tree.root.child.get(1).child.get(3).addChild(1);
tree.root.child.get(1).child.get(3).addChild(3);
tree.printLeafNode(tree.root);
}
}
input
-2 9 17 12 6 7 18 3 2 1 3
// Include header file
#include <iostream>
#include <vector>
using namespace std;
// C++ program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
public: int key;
vector < TreeNode* > child;
TreeNode(int key)
{
this->key = key;
}
void addChild(int key)
{
TreeNode *t = new TreeNode(key);
this->child.push_back(t);
}
};
class NAryTree
{
public: TreeNode *root;
NAryTree()
{
this->root = NULL;
}
// Print all leaf nodes
void printLeafNode(TreeNode *node)
{
if (node == NULL)
{
return;
}
int i = 0;
// iterating the child of given node
while (i < node->child.size())
{
// Recursively visit child node
this->printLeafNode(node->child.at(i));
i++;
}
if (node->child.size() == 0)
{
// When node is left node
cout << " " << node->key;
}
}
};
int main()
{
NAryTree *tree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree->root = new TreeNode(10);
tree->root->addChild(8);
tree->root->addChild(5);
// Add child node [-2,1,5] in node (8)
tree->root->child.at(0)->addChild(-2);
tree->root->child.at(0)->addChild(1);
tree->root->child.at(0)->addChild(6);
// Add child node [9,11] in node (1)
tree->root->child.at(0)->child.at(1)->addChild(9);
tree->root->child.at(0)->child.at(1)->addChild(11);
// Add child node [17 12] in node (11)
tree->root->child.at(0)->child.at(1)->child.at(1)->addChild(17);
tree->root->child.at(0)->child.at(1)->child.at(1)->addChild(12);
// Add child node [7 18 3 4] in node (5)
tree->root->child.at(1)->addChild(7);
tree->root->child.at(1)->addChild(18);
tree->root->child.at(1)->addChild(3);
tree->root->child.at(1)->addChild(4);
// Add child node [2,1,3] in node (7)
tree->root->child.at(1)->child.at(3)->addChild(2);
tree->root->child.at(1)->child.at(3)->addChild(1);
tree->root->child.at(1)->child.at(3)->addChild(3);
tree->printLeafNode(tree->root);
return 0;
}
input
-2 9 17 12 6 7 18 3 2 1 3
// Include namespace system
using System;
using System.Collections.Generic;
// Csharp program for
// Print the all leaf nodes in N-ary tree
public class TreeNode
{
public int key;
public List < TreeNode > child;
public TreeNode(int key)
{
this.key = key;
this.child = new List < TreeNode > ();
}
public void addChild(int key)
{
TreeNode t = new TreeNode(key);
this.child.Add(t);
}
}
public class NAryTree
{
public TreeNode root;
public NAryTree()
{
// Set initial tree root to null
this.root = null;
}
// Print all leaf nodes
public void printLeafNode(TreeNode node)
{
if (node == null)
{
return;
}
int i = 0;
// iterating the child of given node
while (i < node.child.Count)
{
// Recursively visit child node
this.printLeafNode(node.child[i]);
i++;
}
if (node.child.Count == 0)
{
// When node is left node
Console.Write(" " + node.key);
}
}
public static void Main(String[] args)
{
NAryTree tree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = new TreeNode(10);
tree.root.addChild(8);
tree.root.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root.child[0].addChild(-2);
tree.root.child[0].addChild(1);
tree.root.child[0].addChild(6);
// Add child node [9,11] in node (1)
tree.root.child[0].child[1].addChild(9);
tree.root.child[0].child[1].addChild(11);
// Add child node [17 12] in node (11)
tree.root.child[0].child[1].child[1].addChild(17);
tree.root.child[0].child[1].child[1].addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root.child[1].addChild(7);
tree.root.child[1].addChild(18);
tree.root.child[1].addChild(3);
tree.root.child[1].addChild(4);
// Add child node [2,1,3] in node (7)
tree.root.child[1].child[3].addChild(2);
tree.root.child[1].child[3].addChild(1);
tree.root.child[1].child[3].addChild(3);
tree.printLeafNode(tree.root);
}
}
input
-2 9 17 12 6 7 18 3 2 1 3
<?php
// Php program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
public $key;
public $child;
public function __construct($key)
{
$this->key = $key;
$this->child = array();
}
public function addChild($key)
{
$t = new TreeNode($key);
$this->child[] = $t;
}
}
class NAryTree
{
public $root;
public function __construct()
{
$this->root = NULL;
}
// Print all leaf nodes
public function printLeafNode($node)
{
if ($node == NULL)
{
return;
}
$i = 0;
// iterating the child of given node
while ($i < count($node->child))
{
// Recursively visit child node
$this->printLeafNode($node->child[$i]);
$i++;
}
if (count($node->child) == 0)
{
// When node is left node
echo(" ".$node->key);
}
}
}
function main()
{
$tree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
$tree->root = new TreeNode(10);
$tree->root->addChild(8);
$tree->root->addChild(5);
// Add child node [-2,1,5] in node (8)
$tree->root->child[0]->addChild(-2);
$tree->root->child[0]->addChild(1);
$tree->root->child[0]->addChild(6);
// Add child node [9,11] in node (1)
$tree->root->child[0]->child[1]->addChild(9);
$tree->root->child[0]->child[1]->addChild(11);
// Add child node [17 12] in node (11)
$tree->root->child[0]->child[1]->child[1]->addChild(17);
$tree->root->child[0]->child[1]->child[1]->addChild(12);
// Add child node [7 18 3 4] in node (5)
$tree->root->child[1]->addChild(7);
$tree->root->child[1]->addChild(18);
$tree->root->child[1]->addChild(3);
$tree->root->child[1]->addChild(4);
// Add child node [2,1,3] in node (7)
$tree->root->child[1]->child[3]->addChild(2);
$tree->root->child[1]->child[3]->addChild(1);
$tree->root->child[1]->child[3]->addChild(3);
$tree->printLeafNode($tree->root);
}
main();
input
-2 9 17 12 6 7 18 3 2 1 3
// Node JS program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
constructor(key)
{
this.key = key;
this.child = [];
}
addChild(key)
{
var t = new TreeNode(key);
this.child.push(t);
}
}
class NAryTree
{
constructor()
{
this.root = null;
}
// Print all leaf nodes
printLeafNode(node)
{
if (node == null)
{
return;
}
var i = 0;
// iterating the child of given node
while (i < node.child.length)
{
// Recursively visit child node
this.printLeafNode(node.child[i]);
i++;
}
if (node.child.length == 0)
{
// When node is left node
process.stdout.write(" " + node.key);
}
}
}
function main()
{
var tree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = new TreeNode(10);
tree.root.addChild(8);
tree.root.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root.child[0].addChild(-2);
tree.root.child[0].addChild(1);
tree.root.child[0].addChild(6);
// Add child node [9,11] in node (1)
tree.root.child[0].child[1].addChild(9);
tree.root.child[0].child[1].addChild(11);
// Add child node [17 12] in node (11)
tree.root.child[0].child[1].child[1].addChild(17);
tree.root.child[0].child[1].child[1].addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root.child[1].addChild(7);
tree.root.child[1].addChild(18);
tree.root.child[1].addChild(3);
tree.root.child[1].addChild(4);
// Add child node [2,1,3] in node (7)
tree.root.child[1].child[3].addChild(2);
tree.root.child[1].child[3].addChild(1);
tree.root.child[1].child[3].addChild(3);
tree.printLeafNode(tree.root);
}
main();
input
-2 9 17 12 6 7 18 3 2 1 3
# Python 3 program for
# Print the all leaf nodes in N-ary tree
class TreeNode :
def __init__(self, key) :
self.key = key
self.child = []
def addChild(self, key) :
t = TreeNode(key)
self.child.append(t)
class NAryTree :
def __init__(self) :
self.root = None
# Print all leaf nodes
def printLeafNode(self, node) :
if (node == None) :
return
i = 0
# iterating the child of given node
while (i < len(node.child)) :
# Recursively visit child node
self.printLeafNode(node.child[i])
i += 1
if (len(node.child) == 0) :
# When node is left node
print(" ", node.key, end = "")
def main() :
tree = NAryTree()
# 10
# / \
# / \
# / \
# 8 5
# /|\ /|\ \
# / | \ / | \ \
# -2 1 6 7 18 3 4
# / \ /| \
# 9 11 2 1 3
# / \
# 17 12
# -----------------------
# Constructing N-Arr tree
# First element of tree
tree.root = TreeNode(10)
tree.root.addChild(8)
tree.root.addChild(5)
# Add child node [-2,1,5] in node (8)
tree.root.child[0].addChild(-2)
tree.root.child[0].addChild(1)
tree.root.child[0].addChild(6)
# Add child node [9,11] in node (1)
tree.root.child[0].child[1].addChild(9)
tree.root.child[0].child[1].addChild(11)
# Add child node [17 12] in node (11)
tree.root.child[0].child[1].child[1].addChild(17)
tree.root.child[0].child[1].child[1].addChild(12)
# Add child node [7 18 3 4] in node (5)
tree.root.child[1].addChild(7)
tree.root.child[1].addChild(18)
tree.root.child[1].addChild(3)
tree.root.child[1].addChild(4)
# Add child node [2,1,3] in node (7)
tree.root.child[1].child[3].addChild(2)
tree.root.child[1].child[3].addChild(1)
tree.root.child[1].child[3].addChild(3)
tree.printLeafNode(tree.root)
if __name__ == "__main__": main()
input
-2 9 17 12 6 7 18 3 2 1 3
# Ruby program for
# Print the all leaf nodes in N-ary tree
class TreeNode
# Define the accessor and reader of class TreeNode
attr_reader :key, :child
attr_accessor :key, :child
def initialize(key)
self.key = key
self.child = []
end
def addChild(key)
t = TreeNode.new(key)
self.child.push(t)
end
end
class NAryTree
# Define the accessor and reader of class NAryTree
attr_reader :root
attr_accessor :root
def initialize()
self.root = nil
end
# Print all leaf nodes
def printLeafNode(node)
if (node == nil)
return
end
i = 0
# iterating the child of given node
while (i < node.child.length)
# Recursively visit child node
self.printLeafNode(node.child[i])
i += 1
end
if (node.child.length == 0)
# When node is left node
print(" ", node.key)
end
end
end
def main()
tree = NAryTree.new()
# 10
# / \
# / \
# / \
# 8 5
# /|\ /|\ \
# / | \ / | \ \
# -2 1 6 7 18 3 4
# / \ /| \
# 9 11 2 1 3
# / \
# 17 12
# -----------------------
# Constructing N-Arr tree
# First element of tree
tree.root = TreeNode.new(10)
tree.root.addChild(8)
tree.root.addChild(5)
# Add child node [-2,1,5] in node (8)
tree.root.child[0].addChild(-2)
tree.root.child[0].addChild(1)
tree.root.child[0].addChild(6)
# Add child node [9,11] in node (1)
tree.root.child[0].child[1].addChild(9)
tree.root.child[0].child[1].addChild(11)
# Add child node [17 12] in node (11)
tree.root.child[0].child[1].child[1].addChild(17)
tree.root.child[0].child[1].child[1].addChild(12)
# Add child node [7 18 3 4] in node (5)
tree.root.child[1].addChild(7)
tree.root.child[1].addChild(18)
tree.root.child[1].addChild(3)
tree.root.child[1].addChild(4)
# Add child node [2,1,3] in node (7)
tree.root.child[1].child[3].addChild(2)
tree.root.child[1].child[3].addChild(1)
tree.root.child[1].child[3].addChild(3)
tree.printLeafNode(tree.root)
end
main()
input
-2 9 17 12 6 7 18 3 2 1 3
import scala.collection.mutable._;
// Scala program for
// Print the all leaf nodes in N-ary tree
class TreeNode(var key: Int,
var child: ArrayBuffer[TreeNode])
{
def this(key: Int)
{
this(key, new ArrayBuffer[TreeNode]());
}
def addChild(key: Int): Unit = {
var t: TreeNode = new TreeNode(key);
this.child += t;
}
}
class NAryTree(var root: TreeNode)
{
def this()
{
this(null);
}
// Print all leaf nodes
def printLeafNode(node: TreeNode): Unit = {
if (node == null)
{
return;
}
var i: Int = 0;
// iterating the child of given node
while (i < node.child.size)
{
// Recursively visit child node
printLeafNode(node.child(i));
i += 1;
}
if (node.child.size == 0)
{
// When node is left node
print(" " + node.key);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: NAryTree = new NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = new TreeNode(10);
tree.root.addChild(8);
tree.root.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root.child(0).addChild(-2);
tree.root.child(0).addChild(1);
tree.root.child(0).addChild(6);
// Add child node [9,11] in node (1)
tree.root.child(0).child(1).addChild(9);
tree.root.child(0).child(1).addChild(11);
// Add child node [17 12] in node (11)
tree.root.child(0).child(1).child(1).addChild(17);
tree.root.child(0).child(1).child(1).addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root.child(1).addChild(7);
tree.root.child(1).addChild(18);
tree.root.child(1).addChild(3);
tree.root.child(1).addChild(4);
// Add child node [2,1,3] in node (7)
tree.root.child(1).child(3).addChild(2);
tree.root.child(1).child(3).addChild(1);
tree.root.child(1).child(3).addChild(3);
tree.printLeafNode(tree.root);
}
}
input
-2 9 17 12 6 7 18 3 2 1 3
// Swift 4 program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
var key: Int;
var child: [TreeNode?] ;
init(_ key: Int)
{
self.key = key;
self.child = [TreeNode]();
}
func addChild(_ key: Int)
{
let t: TreeNode = TreeNode(key);
self.child.append(t);
}
}
class NAryTree
{
var root: TreeNode? ;
init()
{
self.root = nil;
}
// Print all leaf nodes
func printLeafNode(_ node: TreeNode? )
{
if (node == nil)
{
return;
}
var i: Int = 0;
// iterating the child of given node
while (i < node!.child.count)
{
// Recursively visit child node
self.printLeafNode(node!.child[i]);
i += 1;
}
if (node!.child.count == 0)
{
// When node is left node
print(" ", node!.key, terminator: "");
}
}
}
func main()
{
let tree: NAryTree = NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = TreeNode(10);
tree.root!.addChild(8);
tree.root!.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root!.child[0]!.addChild(-2);
tree.root!.child[0]!.addChild(1);
tree.root!.child[0]!.addChild(6);
// Add child node [9,11] in node (1)
tree.root!.child[0]!.child[1]!.addChild(9);
tree.root!.child[0]!.child[1]!.addChild(11);
// Add child node [17 12] in node (11)
tree.root!.child[0]!.child[1]!.child[1]!.addChild(17);
tree.root!.child[0]!.child[1]!.child[1]!.addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root!.child[1]!.addChild(7);
tree.root!.child[1]!.addChild(18);
tree.root!.child[1]!.addChild(3);
tree.root!.child[1]!.addChild(4);
// Add child node [2,1,3] in node (7)
tree.root!.child[1]!.child[3]!.addChild(2);
tree.root!.child[1]!.child[3]!.addChild(1);
tree.root!.child[1]!.child[3]!.addChild(3);
tree.printLeafNode(tree.root);
}
main();
input
-2 9 17 12 6 7 18 3 2 1 3
// Kotlin program for
// Print the all leaf nodes in N-ary tree
class TreeNode
{
var key: Int;
var child: MutableList<TreeNode> ;
constructor(key: Int)
{
this.key = key;
this.child = mutableListOf<TreeNode>();
}
fun addChild(key: Int): Unit
{
val t: TreeNode = TreeNode(key);
this.child.add(t);
}
}
class NAryTree
{
var root: TreeNode ? ;
constructor()
{
this.root = null;
}
// Print all leaf nodes
fun printLeafNode(node: TreeNode ? ): Unit
{
if (node == null)
{
return;
}
var i: Int = 0;
// iterating the child of given node
while (i < node.child.size)
{
// Recursively visit child node
this.printLeafNode(node.child[i]);
i += 1;
}
if (node.child.size == 0)
{
// When node is left node
print(" " + node.key);
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: NAryTree = NAryTree();
/*
10
/ \
/ \
/ \
8 5
/|\ /|\ \
/ | \ / | \ \
-2 1 6 7 18 3 4
/ \ /| \
9 11 2 1 3
/ \
17 12
-----------------------
Constructing N-Arr tree
*/
// First element of tree
tree.root = TreeNode(10);
tree.root?.addChild(8);
tree.root?.addChild(5);
// Add child node [-2,1,5] in node (8)
tree.root!!.child[0].addChild(-2);
tree.root!!.child[0].addChild(1);
tree.root!!.child[0].addChild(6);
// Add child node [9,11] in node (1)
tree.root!!.child[0].child[1].addChild(9);
tree.root!!.child[0].child[1].addChild(11);
// Add child node [17 12] in node (11)
tree.root!!.child[0].child[1].child[1].addChild(17);
tree.root!!.child[0].child[1].child[1].addChild(12);
// Add child node [7 18 3 4] in node (5)
tree.root!!.child[1].addChild(7);
tree.root!!.child[1].addChild(18);
tree.root!!.child[1].addChild(3);
tree.root!!.child[1].addChild(4);
// Add child node [2,1,3] in node (7)
tree.root!!.child[1].child[3].addChild(2);
tree.root!!.child[1].child[3].addChild(1);
tree.root!!.child[1].child[3].addChild(3);
tree.printLeafNode(tree.root);
}
input
-2 9 17 12 6 7 18 3 2 1 3
Time Complexity Analysis
In the worst case, the traversal of the N-ary tree requires visiting each node once. Since the tree's depth is limited, the time complexity is O(n), where n is the number of nodes in the tree.
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.
New Comment