/*
* @lc app=leetcode.cn id=559 lang=javascript
*
* [559] N 叉树的最大深度
*/
// @lc code=start
/**
* // Definition for a _Node.
* function _Node(val,children) {
* this.val = val === undefined ? null : val;
* this.children = children === undefined ? null : children;
* };
*/
/**
* @param {_Node|null} root
* @return {number}
*/
var maxDepth = function (root) {
// 1. 深度遍历
// if (!root) return 0;
// let maxChildDepth = 0;
// const children = root.children;
// for (const child of children) {
// const childDep = maxDepth(child);
// maxChildDepth = Math.max(maxChildDepth, childDep);
// }
// return maxChildDepth + 1;
// 2. 广度遍历
if (!root) return 0;
const queue = [];
let ans = 0;
queue.push(root);
while (queue.length) {
let size = queue.length;
while (size > 0) {
const node = queue.shift();
const children = node.children;
for (let child of children) {
queue.push(child);
}
size--;
}
ans++;
}
return ans;
};
// @lc code=end