Skip to content

111. Minimum Depth of Binary Tree#24

Open
TORUS0818 wants to merge 1 commit intomainfrom
111
Open

111. Minimum Depth of Binary Tree#24
TORUS0818 wants to merge 1 commit intomainfrom
111

Conversation

@TORUS0818
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@kazukiii kazukiii left a comment

Choose a reason for hiding this comment

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

良いと思います!

- 片方しかない場合は飛ばして先に進めば良かった
- maxとminなので対称性があると思ったのに、、
- と勝手に騙された気分になっていたが、maxの時は子が1つの時、depthが0のものを採用することが無い、というだけの話
- minの場合は逆に必ず0が採用されてしまうので今回のようなことになった
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

逆に0が単位元になるようなカスタムのminを定義してあげるのも別解の1つかもと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど、ただ直感的でないので読む人に優しくはないかもしれませんね。

ところでkazukiiiさんは数学関係の方ですか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いや、自分は数学関係の人ではないです。以前どこかでtorusさんが数学科出身というのを見たため伝わると思って書きました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

本職の方かと身構えました。
結構前の話なので、懐かしい感じでした。

if node.right:
next_node_with_depth.append((node.right, depth + 1))

return int(min_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.

なんかこの行が不安になりました。int(math.inf)の結果はどうなりますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。OverflowErrorになりますね。

確かに気持ち悪いですが、どうすればいいのだろう、、キャストの直前で、事前に更新があったか確認する、とかですかね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rootがNoneのときを事前に弾いているので、min_depthは必ず更新されるので、特段キャストしなくて良いかなと思います。まあコメントで補足しておけば良いかなと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。
これ何故かfloatで返ってくると勘違いしてました。不要でしたね。。

if not root.right:
return self.minDepth(root.left) + 1

return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応, Constraints: The number of nodes in the tree is in the range [0, 10^5].とあるので, 再帰の回数には気をつけた方がいいかもですね

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。
意識にはあったのですが、コメントに書くべきでしたね。

if not node.left and not node.right:
return depth

if node.left:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

None でも突っ込んで、出てきたところで判定という手もあります。

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