Conversation
mamo3gr
reviewed
Jan 29, 2026
| (leaf) | ||
| ``` | ||
| - https://github.com/mamo3gr/arai60/pull/20/changes | ||
| - `入出力が合うかのガチャをやり始めると意識が離れてしまう。`これは難しい問題をバグらせた時についやってしまう。実務だと使えない方法なのでしないようにしたい。 |
There was a problem hiding this comment.
実務でもテストを書くとできてしまうんですよね。
[fyi]
ジャッジシステムでレバガチャをしない
mamo3gr
reviewed
Jan 29, 2026
| if root is None: | ||
| return 0 | ||
|
|
||
| def min_depth_helper(root: TreeNode) -> int: |
There was a problem hiding this comment.
minDepth のヘルパーであることはまあ自明なので、自分なら
Suggested change
| def min_depth_helper(root: TreeNode) -> int: | |
| def helper(root: TreeNode) -> int: |
とします。むしろヘルパー関数を書かず、minDepth 自体で再帰しそうです(引数も戻り値も、親関数と変わらないので)。
mamo3gr
reviewed
Jan 29, 2026
Comment on lines
+21
to
+30
|
|
||
| frontier = collections.deque([(root, 1)]) | ||
| while frontier: | ||
|
|
||
| def is_leaf(node: TreeNode) -> bool: | ||
| return node.left is None and node.right is None | ||
|
|
||
| node, depth = frontier.popleft() | ||
| if is_leaf(node): | ||
| return depth |
There was a problem hiding this comment.
ループの中で毎回関数が生成されそうで、外側で一度だけ作りたいです。
Suggested change
| frontier = collections.deque([(root, 1)]) | |
| while frontier: | |
| def is_leaf(node: TreeNode) -> bool: | |
| return node.left is None and node.right is None | |
| node, depth = frontier.popleft() | |
| if is_leaf(node): | |
| return depth | |
| def is_leaf(node: TreeNode) -> bool: | |
| return node.left is None and node.right is None | |
| frontier = collections.deque([(root, 1)]) | |
| while frontier: | |
| node, depth = frontier.popleft() | |
| if is_leaf(node): | |
| return depth |
自分なら、これくらいシンプルなら関数ではなくbooleanにします。
Suggested change
| frontier = collections.deque([(root, 1)]) | |
| while frontier: | |
| def is_leaf(node: TreeNode) -> bool: | |
| return node.left is None and node.right is None | |
| node, depth = frontier.popleft() | |
| if is_leaf(node): | |
| return depth | |
| frontier = collections.deque([(root, 1)]) | |
| while frontier: | |
| node, depth = frontier.popleft() | |
| is_leaf = node.left is None and node.right is None | |
| if is_leaf: | |
| return depth |
mamo3gr
reviewed
Jan 29, 2026
| if node.right is not None: | ||
| frontier.append((node.right, depth + 1)) | ||
|
|
||
| raise AssertionError("unreachable") |
There was a problem hiding this comment.
[nits]
何が起きたか、だけでなく、どうしたらいいか、これを見たユーザーに次のアクションを指示できると良いと思いました。
nodchip
reviewed
Jan 30, 2026
| - 短い箇所を見つけるので幅優先探索の方がいい | ||
| - 片方だけNoneを見つけてWAを出してしまったので要件をしっかり確認するようにしたい | ||
| - 時間計算量はO(N) | ||
| - 空間計算量はO(logN)だと思う |
There was a problem hiding this comment.
木が平衡だった場合、深さが最小となる葉ノードを見つけたときに、直前の深さのすべてのノードが配列に含まれると思います。深さ log N のノードの数は約 N / 2 個、その親の数は N / 4 個となりますので、空間計算量は O(N) になると思います。
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/minimum-depth-of-binary-tree/
次の問題:https://leetcode.com/problems/merge-two-binary-trees/