304 North Cardinal St.
Dorchester Center, MA 02124

# Maximum Depth of N-ary Tree LeetCode Solution

## Problem – Maximum Depth of N-ary Tree LeetCode Solution

Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

``````Input: root = [1,null,3,2,4,null,5,6]
Output: 3
``````

Example 2:

``````Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: 5
``````

Constraints:

• The total number of nodes is in the range `[0, 104]`.
• The depth of the n-ary tree is less than or equal to `1000`.

### Maximum Depth of N-ary Tree LeetCode Solution in Java

``````public int maxDepth(Node root) {
if(root == null) return 0;

queue.offer(root);

int depth = 0;

while(!queue.isEmpty())
{
int size = queue.size();

for(int i = 0; i < size; i++)
{
Node current = queue.poll();
for(Node child: current.children) queue.offer(child);
}

depth++;
}

return depth;
}
``````

### Maximum Depth of N-ary Tree LeetCode Solution in C++

``````class Solution {
public:
int maxDepth(Node* root) {
if (root == nullptr) return 0;
int depth = 0;
for (auto child : root->children) depth = max(depth, maxDepth(child));
return 1 + depth;
}
};
``````

### Maximum Depth of N-ary Tree LeetCode Solution in Python

``````"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
# Base case
if root == None:
return 0
# Depth level of the tree
depth = 0

# Loops through children array
for child in root.children:
# Compares current depth of depth with a new level of depth
# Sets the biggest value to variable depth
depth = max(depth, self.maxDepth(child))

# As going deeper into the tree increases depth by 1
print ('root ' + str(root.val) + ' depth ' + str(depth + 1))
return depth + 1

# So for the first test case it'll look like
# first call is the call with root 1
# depth = 0 -> second call with child 3 ->
# it has 2 children so there'll be two calls from for loop
# call with child 5 -> sets the for loop with child of 5 which is None (Null)
# this call hits base case and returns 0
# the same happens with child 6
# call to child 6 from the for loop -> child of 6 is None -> returns 0
# depth = max(0, 0) -> depth = 0
# return to one level higher -> call to child 5 reaches return statement ->
# depth = 1
# call to child 6 reaches return statement and it returns 1 ->
# current depth is 1 because child 5 set it -> max(1, 1) -> depth = 1
# call to child 3 reaches return statement ->
# depth was equal to 1 now it becomes 2 (depth + 1)
# now it's turn to children 2 and 4 to be called
# the story is exactly the same as with 5 and 6 only depth is now 2 ->
# both 2 and 4 return depth 1 that's why it's rejected by max(2, 1)
# depth remains 2
# and finally everything returns to the very first call with root 1
# it reaches return statement and returns 2+1=3
``````
##### Maximum Depth of N-ary Tree LeetCode Solution Review:

In our experience, we suggest you solve this Maximum Depth of N-ary Tree LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Maximum Depth of N-ary Tree LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Maximum Depth of N-ary Tree LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions