var maxDepth = function (root) {
let depth = 0;
let res = 0;
function traverse(root) {
if (!root) return;
depth++;
res = Math.max(res, depth);
for (let node of root.children) {
traverse(node);
}
depth--;
}
traverse(root);
return res;
};
var maxDepth = function (root) {
if (!root) return 0;
let depth = 0;
let q = [root];
while (q.length) {
let size = q.length;
for (let i = 0; i < size; i++) {
let cur = q.shift();
if (cur && cur.children) {
for (let node of cur.children) {
q.push(node);
}
}
}
depth++; // 到这里,说明需要遍历下一层了
}
return depth;
};