Skip to main content
Back to problems
#559
Easy Algorithms

Maximum depth of n ary tree

Tree Depth-First Search Breadth-First Search
73.5% acceptance
Jan 13, 2026
2865
97
Given a n-ary tree, find its maximum depth.

Solution

C++
Time O(1)
Space O(1)
LeetCode
solution.cpp
class Solution {
public:
  int maxDepth(Node* root) {
    if (!root) return 0;
    int depth = 0;
    for (Node* child : root->children)
      depth = max(depth, maxDepth(child));
    return depth + 1;
  }
};