559. N 叉树的最大深度
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
var maxDepth = function(root) { let depth = 0 if (root === null) return depth const nodeQues = [root] while(nodeQues.length > 0) { const nodes = nodeQues.splice(0) const nodeLen = nodes.length depth++ for(let i = 0; i < nodeLen; i++) { const node = nodes[i] const { children = [] } = node nodeQues.push(...children.filter(data => data !== null)) } } return depth };
|