Conversation
| - 片方しかない場合は飛ばして先に進めば良かった | ||
| - maxとminなので対称性があると思ったのに、、 | ||
| - と勝手に騙された気分になっていたが、maxの時は子が1つの時、depthが0のものを採用することが無い、というだけの話 | ||
| - minの場合は逆に必ず0が採用されてしまうので今回のようなことになった |
There was a problem hiding this comment.
逆に0が単位元になるようなカスタムのminを定義してあげるのも別解の1つかもと思いました。
There was a problem hiding this comment.
なるほど、ただ直感的でないので読む人に優しくはないかもしれませんね。
ところでkazukiiiさんは数学関係の方ですか?
There was a problem hiding this comment.
いや、自分は数学関係の人ではないです。以前どこかでtorusさんが数学科出身というのを見たため伝わると思って書きました。
There was a problem hiding this comment.
本職の方かと身構えました。
結構前の話なので、懐かしい感じでした。
| if node.right: | ||
| next_node_with_depth.append((node.right, depth + 1)) | ||
|
|
||
| return int(min_depth) |
There was a problem hiding this comment.
なんかこの行が不安になりました。int(math.inf)の結果はどうなりますか?
There was a problem hiding this comment.
有難うございます。OverflowErrorになりますね。
確かに気持ち悪いですが、どうすればいいのだろう、、キャストの直前で、事前に更新があったか確認する、とかですかね。
There was a problem hiding this comment.
rootがNoneのときを事前に弾いているので、min_depthは必ず更新されるので、特段キャストしなくて良いかなと思います。まあコメントで補足しておけば良いかなと思います。
There was a problem hiding this comment.
有難うございます。
これ何故かfloatで返ってくると勘違いしてました。不要でしたね。。
| if not root.right: | ||
| return self.minDepth(root.left) + 1 | ||
|
|
||
| return min(self.minDepth(root.left), self.minDepth(root.right)) + 1 |
There was a problem hiding this comment.
一応, Constraints: The number of nodes in the tree is in the range [0, 10^5].とあるので, 再帰の回数には気をつけた方がいいかもですね
There was a problem hiding this comment.
有難うございます。
意識にはあったのですが、コメントに書くべきでしたね。
| if not node.left and not node.right: | ||
| return depth | ||
|
|
||
| if node.left: |
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/