Given an n-ary tree containing positive node values, the task is to find the depth of the tree.
Note: An n-ary tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), the n-ary tree allows for multiple branches or children for each node.
Examples:
Input:

Output: 3
Explanation: The longest path from the root (node 81) to a leaf is either 81 -> 26 -> 95 or 81 -> 26 -> 86, giving a maximum depth of 3.
Input:
Output: 2
Explanation: The longest path from the root (node 4) to any leaf (nodes 5 or 7) is 2, as it only requires two levels of traversal.
Approach:
The idea is to calculate the depth of an N-ary tree recursively, initialize the maximum depth as 0, then recursively calculate the depth for each child and keep track of the highest depth encountered. Finally, add 1 to the maximum depth (for the current node) and return the result. This approach ensures that we find the longest path from the root to any leaf node.
N-Ary tree can be traversed just like a normal tree. We just have to consider all children of a given node and recursively call that function on every node.
C++
// C++ Code to find the depth of an N-ary tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
vector<Node*> children;
Node(int val) {
data = val;
}
};
// Recursive function to calculate maximum depth
int maxDepth(Node* root) {
// If the node is null, depth is 0
if (!root) {
return 0;
}
int depth = 0;
// Recur for all children and find the maximum depth
for (auto child : root->children) {
depth = max(depth, maxDepth(child));
}
// Add 1 to include the current node
// in the depth count
return depth + 1;
}
int main() {
// Representation of given N-ary tree
// 1
// / | \
// 2 3 4
// / \
// 5 6
Node* root = new Node(1);
root->children.push_back(new Node(2));
root->children.push_back(new Node(3));
root->children.push_back(new Node(4));
root->children[0]->children.push_back(new Node(5));
root->children[2]->children.push_back(new Node(6));
cout << maxDepth(root);
return 0;
}
Java
// Java Code to find the depth of an N-ary tree
import java.util.*;
class Node {
int data;
List<Node> children;
Node(int val) {
data = val;
children = new ArrayList<>();
}
}
// Recursive function to calculate
// maximum depth
class GfG {
static int maxDepth(Node root) {
// If the node is null, depth is 0
if (root == null) {
return 0;
}
int depth = 0;
// Recur for all children and find
// the maximum depth
for (Node child : root.children) {
depth = Math.max(depth, maxDepth(child));
}
// Add 1 to include the current node
// in the depth count
return depth + 1;
}
public static void main(String[] args) {
// Representation of given N-ary tree
// 1
// / | \
// 2 3 4
// / \
// 5 6
Node root = new Node(1);
root.children.add(new Node(2));
root.children.add(new Node(3));
root.children.add(new Node(4));
root.children.get(0).children.add(new Node(5));
root.children.get(2).children.add(new Node(6));
System.out.println(maxDepth(root));
}
}
Python
# Python Code to find the depth
# of an N-ary tree
class Node:
def __init__(self, val):
self.data = val
self.children = []
# Recursive function to calculate
# maximum depth
def max_depth(root):
# If the node is None, depth is 0
if not root:
return 0
depth = 0
# Recur for all children and
# find the maximum depth
for child in root.children:
depth = max(depth, max_depth(child))
# Add 1 to include the current
# node in the depth count
return depth + 1
if __name__ == "__main__":
# Representation of given N-ary tree
# 1
# / | \
# 2 3 4
# / \
# 5 6
root = Node(1)
root.children.append(Node(2))
root.children.append(Node(3))
root.children.append(Node(4))
root.children[0].children.append(Node(5))
root.children[2].children.append(Node(6))
print(max_depth(root))
C#
// C# Code to find the depth of an N-ary tree
using System;
using System.Collections.Generic;
class Node {
public int data;
public List<Node> children;
public Node(int val) {
data = val;
children = new List<Node>();
}
}
// Recursive function to calculate
// maximum depth
class GfG {
static int MaxDepth(Node root) {
// If the node is null, depth is 0
if (root == null) {
return 0;
}
int depth = 0;
// Recur for all children and find the maximum depth
foreach (Node child in root.children) {
depth = Math.Max(depth, MaxDepth(child));
}
// Add 1 to include the current
// node in the depth count
return depth + 1;
}
static void Main(string[] args) {
// Representation of given N-ary tree
// 1
// / | \
// 2 3 4
// / \
// 5 6
Node root = new Node(1);
root.children.Add(new Node(2));
root.children.Add(new Node(3));
root.children.Add(new Node(4));
root.children[0].children.Add(new Node(5));
root.children[2].children.Add(new Node(6));
Console.WriteLine(MaxDepth(root));
}
}
JavaScript
// JavaScript Code to find the depth
// of an N-ary tree
class Node {
constructor(val) {
this.data = val;
this.children = [];
}
}
// Recursive function to calculate
// maximum depth
function maxDepth(root) {
// If the node is null, depth is 0
if (!root) {
return 0;
}
let depth = 0;
// Recur for all children and find
// the maximum depth
for (let child of root.children) {
depth = Math.max(depth, maxDepth(child));
}
// Add 1 to include the current node
// in the depth count
return depth + 1;
}
// Representation of given N-ary tree
// 1
// / | \
// 2 3 4
// / \
// 5 6
const root = new Node(1);
root.children.push(new Node(2));
root.children.push(new Node(3));
root.children.push(new Node(4));
root.children[0].children.push(new Node(5));
root.children[2].children.push(new Node(6));
console.log(maxDepth(root));
Time Complexity: O(n), since each node is visited once, where n is the total number of nodes in the N-ary tree.
Auxiliary Space: O(h), where h is the height of the tree, due to recursive call stack usage.
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem