Skip to content

111. Minimum Depth of Binary Tree#24

Open
xbam326 wants to merge 3 commits into
mainfrom
111
Open

111. Minimum Depth of Binary Tree#24
xbam326 wants to merge 3 commits into
mainfrom
111

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 28, 2026

(leaf)
```
- https://github.com/mamo3gr/arai60/pull/20/changes
- `入出力が合うかのガチャをやり始めると意識が離れてしまう。`これは難しい問題をバグらせた時についやってしまう。実務だと使えない方法なのでしないようにしたい。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実務でもテストを書くとできてしまうんですよね。

[fyi]

ジャッジシステムでレバガチャをしない

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.ofzx0kw1dsrd

if root is None:
return 0

def min_depth_helper(root: TreeNode) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minDepth のヘルパーであることはまあ自明なので、自分なら

Suggested change
def min_depth_helper(root: TreeNode) -> int:
def helper(root: TreeNode) -> int:

とします。むしろヘルパー関数を書かず、minDepth 自体で再帰しそうです(引数も戻り値も、親関数と変わらないので)。

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ループの中で毎回関数が生成されそうで、外側で一度だけ作りたいです。

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

if node.right is not None:
frontier.append((node.right, depth + 1))

raise AssertionError("unreachable")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
何が起きたか、だけでなく、どうしたらいいか、これを見たユーザーに次のアクションを指示できると良いと思いました。

- 短い箇所を見つけるので幅優先探索の方がいい
- 片方だけNoneを見つけてWAを出してしまったので要件をしっかり確認するようにしたい
- 時間計算量はO(N)
- 空間計算量はO(logN)だと思う
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

木が平衡だった場合、深さが最小となる葉ノードを見つけたときに、直前の深さのすべてのノードが配列に含まれると思います。深さ log N のノードの数は約 N / 2 個、その親の数は N / 4 個となりますので、空間計算量は O(N) になると思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants