-
Notifications
You must be signed in to change notification settings - Fork 0
111. Minimum Depth of Binary Tree #23
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
base: main
Are you sure you want to change the base?
Conversation
| if not node.left and not node.right: | ||
| min_depth = min(min_depth, depth) | ||
|
|
||
| if node.left: |
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.
わりと好みの範囲だと思いますが、自分はleftとrightの処理を逆にします。ツリーの探索は左から見るのが自然だと思うので。今のコードだと右から探索しています。
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.
ああ、たしかにそうですね。そこまで気が回っていませんでした、ありがとうございます。
| # self.right = right | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| nodes_to_visit = deque([(root, 1)]) |
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.
好みの問題かもしれませんが、nodes_to_visitよりも、queueの中身をより反映させるために、nodes_and_depthに自分ならするかもしれません。
|
|
||
| min_depth = math.inf | ||
| if root.left: | ||
| min_depth = min(min_depth, self.minDepth(root.left)) |
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.
見やすさのために書いているかもしれませんが、ここはmin_depth = self.minDepth(root.left)でも通りそうですね。
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.
たしかにそれでもいいですね。
自分は関数型言語でいうところのfoldのイメージで書いてました。
単位元で初期化して、二項関数で一つにまとめていくイメージです。
| return depth | ||
| nodes_to_visit.append((node.left, depth + 1)) | ||
| nodes_to_visit.append((node.right, depth + 1)) | ||
| return 0 |
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のときにしかここに到達しない旨のコメントはあってもいいかもなと思いました。
nodeがNoneのとき、leafのとき、内部節点のときそれぞれの場合で処理を考えないとそれが分からず、分からないと0を返して本当にいいんだっけ?と不安になるからです。
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の場合を弾いた方が読みやすいでしょうか?
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
nodes_to_visit = deque([(root, 1)])
while nodes_to_visit:
node, depth = nodes_to_visit.popleft()
if not node: continue
if not node.left and not node.right:
return depth
nodes_to_visit.append((node.left, depth + 1))
nodes_to_visit.append((node.right, depth + 1))
raise ValueError("unreachable")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.
コメントにするか先に弾くかは趣味の範囲な気が自分はしました。どちらも読みやすいと思います!
Yoshiki-Iwasa
left a 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.
よさそうです!
問題へのリンク
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
次に解く問題
617. Merge Two Binary Trees
README.mdへ頭の中の言語化と記録をしています。