-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
if root is None: | ||
return 0 | ||
|
||
if root.left is not None and root.right is not None: |
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.
is not Noneという条件、なくても問題なさそうに見えます。
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.
すいません、間違えました、これは必要ですね。
訂正箇所を間違えました
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.
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: |
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.
L10-L11 は L12-L14 と処理が被っているので、L12-L14を修正することでなくせそうです。
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.
すいません、間違えました。
これをやろうとすると、rootがNoneのときの処理が面倒になるので、ここはこのままで良さそうです
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.
無理やりやろうとすると、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 |
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.
自分はinfを使いたくなるのですが、infはfloat型なので抵抗がある方もいらっしゃるようです
fhiyo/leetcode#41 (comment)
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.
マジックナンバーは避けたいという気持ちもあります。
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: |
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.
ここ、True にしてもいいと思います。抜けることはないので。
return 1 | ||
``` | ||
|
||
list二つで行う階層型BFS |
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.
好みかもですが、私もBFSは、レベル別で管理した2つのリストでやる派です。
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/