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).

&nbsp;
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


&nbsp;
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.


class Solution:
    def maxDepth(self, root: 'Node') -> int:
        
        if not root : return 0
        
        if root.children :
            return 1 + max([self.maxDepth(x) for x in root.children])
        else :
            return 1 
class Solution {
    public int maxDepth(Node root) {
        if (root == null) return 0;
        int[] max = new int[]{0};
        dfs(root,1,max);
        return max[0];
    }
    public static void dfs(Node root, int depth, int[] max) {
        if (depth>max[0]) max[0] = depth;
        if(root==null){
            return;
        }
        ++depth;
        for(Node n:root.children) dfs(n, depth, max);
    }
}
class Solution {
public:
    int maxDepth(Node* root) 
    {
        if(root == NULL)
        {
            return 0;
        }
        int dep = 1, mx = INT_MIN;
        helper(root, dep, mx);
        return mx;
    }
    
    void helper(Node *root, int dep, int& mx)
    {
        if(root->children.size() == 0)
        {
            mx = max(mx, dep);
        }
        for(int i = 0 ; i<root->children.size() ; i++)
        {
            helper(root->children[i], dep+1, mx);
        }
    }
};
/**
 * // Definition for a Node.
 * function Node(val,children) {
 * this.val = val;
 * this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number}
 */
var maxDepth = function(root) {
  let max = 0;

  if (!root) {
    return max;
  }

  const search = (root, index) => {
    max = Math.max(index, max);

    if (root?.children && root?.children.length > 0) {
      for (let i = 0; i < root.children.length; i++) {
        search(root.children[i], index+1);
      }
    }
  }

  search(root, 1);

  return max;
};
Maximum Depth of N-ary Tree