Maximum Depth of N-ary Tree LeetCode solution in JavaScript
1/**2* // Definition for a Node.3* function Node(val,children) {4* this.val = val;5* this.children = children;6* };7*/89/**10* @param {Node|null} root11* @return {number}12*/13var maxDepth = function (root) {14let max = 0;1516if (!root) {17return max;18}1920const search = (root, index) => {21max = Math.max(index, max);2223if (root?.children && root?.children.length > 0) {24for (let i = 0; i < root.children.length; i++) {25search(root.children[i], index + 1);26}27}28};2930search(root, 1);3132return max;33};