Open
Conversation
liquo-rice
reviewed
Jun 18, 2024
| if root is None: | ||
| return 0 | ||
| depth = 0 | ||
| nodes = [root] |
There was a problem hiding this comment.
nodes = []
append_if_exists(node, nodes)| ```py | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| def append_if_exists(node: Optional[TreeNode], nodes: list[TreeNode]): |
There was a problem hiding this comment.
引数の順番は、コンテナ、アイテムの順番が一般的かもしれません。(上ではそうなってますね)
https://docs.python.org/3/library/heapq.html#heapq.heappush
| - 空間計算量: O(h) | ||
|
|
||
| ```py | ||
| class Solution: |
Owner
Author
There was a problem hiding this comment.
なるべく再現するとこんな感じですかね。
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
max_depth = 0
def max_depth_helper(node: Optional[TreeNode], depth: int):
nonlocal max_depth
max_depth = max(max_depth, depth)
if node.left is not None:
max_depth_helper(node.left, depth + 1)
if node.right is not None:
max_depth_helper(node.right, depth + 1)
max_depth_helper(root, 1)
return max_depth少し整理してみたらこうなりました。
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
def max_depth_helper(node: Optional[TreeNode], depth: int):
nonlocal max_depth
max_depth = max(max_depth, depth)
if node is None:
return
max_depth_helper(node.left, depth + 1)
max_depth_helper(node.right, depth + 1)
max_depth_helper(root, 0)
return max_depth
oda
reviewed
Jun 19, 2024
| return | ||
| nodes.append(node) | ||
|
|
||
| if root is None: |
Owner
Author
There was a problem hiding this comment.
や、問題設定上 The number of nodes in the tree is in the range [0, 10^4]. なので、入力でrootがNoneな場合があり得ますね (そして node.left のところでエラーになる)
TORUS0818
reviewed
Jul 12, 2024
| - https://discord.com/channels/1084280443945353267/1227073733844406343/1236232864098684928 | ||
| - https://discord.com/channels/1084280443945353267/1227073733844406343/1236695050902048899 | ||
|
|
||
| 帰りがけの処理を行う再帰をスタックで実装したバージョン。 |
There was a problem hiding this comment.
個人的にやや複雑な処理だなと感じたんですが、割とサラッと実装できる感じでしょうか。
Owner
Author
There was a problem hiding this comment.
あまり覚えてないんですが、そこまでさらっとは出来なかった気がします...
nodchip
reviewed
Jul 12, 2024
| if root is None: | ||
| return 0 | ||
| depth = 0 | ||
| nodes = [root] |
There was a problem hiding this comment.
個人的には deque に (node, depth) を入れ、先頭から取り出し、末尾に入れていくタイプの実装のほうが好きですが、fhiyo さんの実装でもよいと思います。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/