-
Notifications
You must be signed in to change notification settings - Fork 0
104 maximum depth of binary tree #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kitano-kazuki
wants to merge
5
commits into
main
Choose a base branch
from
104-maximum-depth-of-binary-tree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 +1,196 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| # Step1 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## アプローチ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| * 最も深い場所を知りたい | ||||||||||||||||||||||||||||||||||||||||||||||||
| * BFSかDFS | ||||||||||||||||||||||||||||||||||||||||||||||||
| * これは全てのノードを訪問する必要があるのでO(N) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code1-1 (DFS) - solved 3:52 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| node = root | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier = [(node, 1)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| maximum_depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| while frontier: | ||||||||||||||||||||||||||||||||||||||||||||||||
| node, depth = frontier.pop() | ||||||||||||||||||||||||||||||||||||||||||||||||
| maximum_depth = max(maximum_depth, depth) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node.left is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append((node.left, depth + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node.right is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append((node.right, depth + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return maximum_depth | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code1-2 (Recursion) - solved 0:46 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| left_max_depth = self.maxDepth(root.left) | ||||||||||||||||||||||||||||||||||||||||||||||||
| right_max_depth = self.maxDepth(root.right) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return max(left_max_depth, right_max_depth) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code1-3 (BFS) - solved 2:12 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import deque | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| frontier = deque() | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(root) | ||||||||||||||||||||||||||||||||||||||||||||||||
| depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| while frontier: | ||||||||||||||||||||||||||||||||||||||||||||||||
| num_cur_frontiers = len(frontier) | ||||||||||||||||||||||||||||||||||||||||||||||||
| depth += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
| for _ in range(num_cur_frontiers): | ||||||||||||||||||||||||||||||||||||||||||||||||
| node = frontier.popleft() | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node.left is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(node.left) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node.right is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(node.right) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return depth | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # 他の人のコードや調べたこと | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| * 1 - https://github.com/n6o/leetcode_arai60/pull/20/changes | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 思いついた解法3種類全部一緒だった | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 実装もほぼ一緒 | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 2 - https://github.com/TakayaShirai/leetcode_practice/pull/21 | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 同様に解法3種類やっている | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 3 - https://github.com/Hiroto-Iizuka/coding_practice/pull/21 | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Pythonだと再帰に忌避感を持っていた方がいいかも | ||||||||||||||||||||||||||||||||||||||||||||||||
| * > 個人的にはPythonで再帰を書くことにかなり抵抗があるので、iterativeでもこの程度のコード量で済むならiterativeで書きたいと思いました。 | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 4 - https://github.com/xbam326/leetcode/pull/23 | ||||||||||||||||||||||||||||||||||||||||||||||||
| * 5 - https://github.com/dxxsxsxkx/leetcode/pull/21 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # Step2 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code2-1 (DFDS) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| node = root | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier = [(node, 1)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| maximum_depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| while frontier: | ||||||||||||||||||||||||||||||||||||||||||||||||
| node, depth = frontier.pop() | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||
| maximum_depth = max(maximum_depth, depth) | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append((node.left, depth + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append((node.right, depth + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return maximum_depth | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code2-2 (Recursion) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ## Code2-3(BFS) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import deque | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class TreeNode: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, val=0, left=None, right=None): | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.left = left | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.right = right | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if root is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| frontier = deque() | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(root) | ||||||||||||||||||||||||||||||||||||||||||||||||
| depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| while frontier: | ||||||||||||||||||||||||||||||||||||||||||||||||
| num_cur_frontiers = len(frontier) | ||||||||||||||||||||||||||||||||||||||||||||||||
| depth += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
| for _ in range(num_cur_frontiers): | ||||||||||||||||||||||||||||||||||||||||||||||||
| node = frontier.popleft() | ||||||||||||||||||||||||||||||||||||||||||||||||
| if node is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(node.left) | ||||||||||||||||||||||||||||||||||||||||||||||||
| frontier.append(node.right) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+179
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. キューに異なる階層のノードが入っていて、かつそれらの深さはキュー外で管理されている、という構造がちょっと違和感がありました。私なら、いま見る階層と、次に見る階層を分けてスワップします。
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return depth | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # Step3 | ||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or 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,24 @@ | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| node = root | ||
| frontier = [(node, 1)] | ||
| maximum_depth = 0 | ||
| while frontier: | ||
| node, depth = frontier.pop() | ||
| maximum_depth = max(maximum_depth, depth) | ||
| if node.left is not None: | ||
| frontier.append((node.left, depth + 1)) | ||
| if node.right is not None: | ||
| frontier.append((node.right, depth + 1)) | ||
|
|
||
| return maximum_depth | ||
|
|
This file contains hidden or 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,14 @@ | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| left_max_depth = self.maxDepth(root.left) | ||
| right_max_depth = self.maxDepth(root.right) | ||
| return max(left_max_depth, right_max_depth) + 1 |
This file contains hidden or 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,27 @@ | ||
| from collections import deque | ||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| frontier = deque() | ||
| frontier.append(root) | ||
| depth = 0 | ||
| while frontier: | ||
| num_cur_frontiers = len(frontier) | ||
| depth += 1 | ||
| for _ in range(num_cur_frontiers): | ||
| node = frontier.popleft() | ||
| if node.left is not None: | ||
| frontier.append(node.left) | ||
| if node.right is not None: | ||
| frontier.append(node.right) | ||
|
|
||
| return depth |
This file contains hidden or 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,26 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| node = root | ||
| frontier = [(node, 1)] | ||
| maximum_depth = 0 | ||
| while frontier: | ||
| node, depth = frontier.pop() | ||
| if node is None: | ||
| continue | ||
| maximum_depth = max(maximum_depth, depth) | ||
| frontier.append((node.left, depth + 1)) | ||
| frontier.append((node.right, depth + 1)) | ||
|
|
||
| return maximum_depth |
This file contains hidden or 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,15 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 |
This file contains hidden or 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,29 @@ | ||
| from typing import Optional | ||
| from collections import deque | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| frontier = deque() | ||
| frontier.append(root) | ||
| depth = 0 | ||
| while frontier: | ||
| num_cur_frontiers = len(frontier) | ||
| depth += 1 | ||
| for _ in range(num_cur_frontiers): | ||
| node = frontier.popleft() | ||
| if node.left is not None: | ||
| frontier.append(node.left) | ||
| if node.right is not None: | ||
| frontier.append(node.right) | ||
|
|
||
| return depth |
This file contains hidden or 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,26 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| frontier = [(root, 1)] | ||
| max_depth = 0 | ||
| while frontier: | ||
| node, depth = frontier.pop() | ||
| max_depth = max(depth, max_depth) | ||
| if node.left is not None: | ||
| frontier.append((node.left, depth + 1)) | ||
| if node.right is not None: | ||
| frontier.append((node.right, depth + 1)) | ||
|
|
||
| return max_depth | ||
|
|
This file contains hidden or 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,15 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
また、 frontiers を単数にするか複数にするかについては、こちらのコメントをご参照ください。
achotto/arai60#6 (comment)
また、 current という単語については、こちらのコメントをご参照ください。
Mike0121/LeetCode#7 (comment)
自分なら num_nodes_in_level と名付けると思います。