File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 443
443
556|[ Next Greater Element III] ( ./0556-next-greater-element-iii.js ) |Medium|
444
444
557|[ Reverse Words in a String III] ( ./0557-reverse-words-in-a-string-iii.js ) |Easy|
445
445
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|
446
447
560|[ Subarray Sum Equals K] ( ./0560-subarray-sum-equals-k.js ) |Medium|
447
448
563|[ Binary Tree Tilt] ( ./0563-binary-tree-tilt.js ) |Easy|
448
449
565|[ Array Nesting] ( ./0565-array-nesting.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments