Skip to content
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

Add 111. Minimum Depth of Binary Tree.md #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add 111. Minimum Depth of Binary Tree.md #22

wants to merge 1 commit into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Feb 1, 2025

if root is None:
return 0

if root.left is not None and root.right is not None:
Copy link

Choose a reason for hiding this comment

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

is not Noneという条件、なくても問題なさそうに見えます。

Copy link

@colorbox colorbox Feb 1, 2025

Choose a reason for hiding this comment

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

すいません、間違えました、これは必要ですね。
訂正箇所を間違えました

Copy link
Owner Author

Choose a reason for hiding this comment

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

is not Noneに関しては、なくても動きますが、google style guideに以下のようにあり、それを参考にしてます

Always use if foo is None: (or is not None) to check for a None value. E.g., when testing whether a variable or argument that defaults to None was set to some other value. The other value might be a value that’s false in a boolean context!

if root is None:
return 0

if root.left is not None and root.right is not None:
Copy link

Choose a reason for hiding this comment

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

L10-L11 は L12-L14 と処理が被っているので、L12-L14を修正することでなくせそうです。

Copy link

Choose a reason for hiding this comment

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

すいません、間違えました。
これをやろうとすると、rootがNoneのときの処理が面倒になるので、ここはこのままで良さそうです

Copy link
Owner Author

Choose a reason for hiding this comment

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

無理やりやろうとすると、L10-L16をしたみたいに書き換える感じですかね

max_num_of_nodes = 10 ** 5
left_depth = max_num_of_nodes
if root.left is not None:
    left_depth = self.minDepth(root.left)
right_depth = max_num_of_nodes
if root.right is not None:
    right_depth = self.minDepth(root.right)
return min(left_depth, right_depth) + 1

if root is None:
return 0

max_num_of_nodes = 10 ** 5
Copy link

Choose a reason for hiding this comment

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

自分はinfを使いたくなるのですが、infはfloat型なので抵抗がある方もいらっしゃるようです
fhiyo/leetcode#41 (comment)

Copy link

Choose a reason for hiding this comment

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

マジックナンバーは避けたいという気持ちもあります。
INT_MAX とかのほうが気分がいいですね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.43n5ir9lkgax


nodes_in_depth = [root]
depth = 1
while nodes_in_depth:
Copy link

Choose a reason for hiding this comment

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

ここ、True にしてもいいと思います。抜けることはないので。

return 1
```

list二つで行う階層型BFS

Choose a reason for hiding this comment

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

好みかもですが、私もBFSは、レベル別で管理した2つのリストでやる派です。

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.

5 participants