Skip to content

Commit 6fd0d17

Browse files
committed
Add solution #559
1 parent 4059a0f commit 6fd0d17

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@
443443
556|[Next Greater Element III](./0556-next-greater-element-iii.js)|Medium|
444444
557|[Reverse Words in a String III](./0557-reverse-words-in-a-string-iii.js)|Easy|
445445
558|[Logical OR of Two Binary Grids Represented as Quad-Trees](./0558-logical-or-of-two-binary-grids-represented-as-quad-trees.js)|Medium|
446+
559|[Maximum Depth of N-ary Tree](./0559-maximum-depth-of-n-ary-tree.js)|Easy|
446447
560|[Subarray Sum Equals K](./0560-subarray-sum-equals-k.js)|Medium|
447448
563|[Binary Tree Tilt](./0563-binary-tree-tilt.js)|Easy|
448449
565|[Array Nesting](./0565-array-nesting.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 559. Maximum Depth of N-ary Tree
3+
* https://leetcode.com/problems/maximum-depth-of-n-ary-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given a n-ary tree, find its maximum depth.
7+
*
8+
* The maximum depth is the number of nodes along the longest path from the root node down to
9+
* the farthest leaf node.
10+
*
11+
* Nary-Tree input serialization is represented in their level order traversal, each group of
12+
* children is separated by the null value (See examples).
13+
*/
14+
15+
/**
16+
* // Definition for a _Node.
17+
* function _Node(val,children) {
18+
* this.val = val === undefined ? null : val;
19+
* this.children = children === undefined ? null : children;
20+
* };
21+
*/
22+
23+
/**
24+
* @param {_Node|null} root
25+
* @return {number}
26+
*/
27+
var maxDepth = function(root) {
28+
if (!root) return 0;
29+
if (!root.children || !root.children.length) return 1;
30+
return Math.max(...root.children.map(child => maxDepth(child))) + 1;
31+
};

0 commit comments

Comments
 (0)