-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problems: No.0559,0563 (#3948)
- Loading branch information
Showing
17 changed files
with
377 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
""" | ||
# Definition for a Node. | ||
class Node: | ||
def __init__(self, val=None, children=None): | ||
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): | ||
self.val = val | ||
self.children = children | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxDepth(self, root: 'Node') -> int: | ||
def maxDepth(self, root: "Node") -> int: | ||
if root is None: | ||
return 0 | ||
return 1 + max([self.maxDepth(child) for child in root.children], default=0) | ||
mx = 0 | ||
for child in root.children: | ||
mx = max(mx, self.maxDepth(child)) | ||
return 1 + mx |
19 changes: 19 additions & 0 deletions
19
solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Definition for _Node. | ||
* class _Node { | ||
* val: number | ||
* children: _Node[] | ||
* | ||
* constructor(val?: number, children?: _Node[]) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.children = (children===undefined ? [] : children) | ||
* } | ||
* } | ||
*/ | ||
|
||
function maxDepth(root: _Node | null): number { | ||
if (!root) { | ||
return 0; | ||
} | ||
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.