Skip to content

Conversation

@kazukiii
Copy link
Owner

@kazukiii kazukiii commented Jul 7, 2024

問題へのリンク
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

次に解く問題
111. Minimum Depth of Binary Tree

README.mdへ頭の中の言語化と記録をしています。

Copy link

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

良いと思います!
一応、BFSはキューにdepthを入れなくても書けることを確認してもよいかもです

- DFS
- 左部分木と右部分木のdepthのmaxを取って1を追加すれば良さそう
- 帰りがけで計算したいので再帰で書きたいところ
- 再帰の深さが最大10^4になるのが気になるが、recursionlimitが550000に設定されているので大丈夫そう
Copy link

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.

たしかにそうですね。ちょっと感覚をアップデートします。

node, depth = nodes_to_visit.popleft()
if not node: continue
max_depth = max(max_depth, depth)
nodes_to_visit.append((node.left, depth + 1))

Choose a reason for hiding this comment

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

自分ならnode.leftがNoneのときはnodes_to_visitに入れないですかね。

Copy link

Choose a reason for hiding this comment

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

これ、良し悪しで None が入らないとすると、root が None でないことも確認する必要がありますね。

Choose a reason for hiding this comment

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

たしかに、そっちもなおさないとですね。

@kazukiii
Copy link
Owner Author

kazukiii commented Jul 8, 2024

@fhiyo
ありがとうございます。BFSのdepthをキューに入れない書き方も追加してみました -> 031e0bf

if node.right:
nodes_to_visit.append(node.right)

return max_depth
Copy link

Choose a reason for hiding this comment

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

ただの感想ですみませんが、この選択肢なかったので勉強になりました🙏

Choose a reason for hiding this comment

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

私もご指摘いただいたのですが,
pop後にnodeがNoneかどうかを確認する方法もありますね.
https://discord.com/channels/1084280443945353267/1218740927120674977/1259562704201318492

Copy link
Owner Author

Choose a reason for hiding this comment

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

@NobukiFukui
レビューありがとうございます。
こちらのキューにdepthを入れない方のBFSの書き方だと、nodeがNoneの場合はキューに入れてはだめな気がしています。

def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Choose a reason for hiding this comment

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

遅くなってしまい申し訳ございませんが,確認いたしました.
全般的によくかけていると思います
再帰の部分ですが,私は左の木の深さと右の木の深さを明示して書いてました.
(どっちが見やすいですかね・・?)

left_depth = self.maxDepth(root.left)
        right_depth = self.maxDepth(root.right)
        depth = max(left_depth, right_depth) + 1
        return depth

Copy link
Owner Author

Choose a reason for hiding this comment

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

左の木の深さと右の木の深さを明示して書いても見やすいですね。ありがとうございます。

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.

7 participants