Skip to content
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 617. Merge Two Binary Trees.md #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add 617. Merge Two Binary Trees.md #23

wants to merge 1 commit into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Mar 1, 2025

- https://github.com/hroc135/leetcode/pull/22/files#r1826509019
- stackに入りうるサイズの概算
- > 事前に確保する配列の大きさをどの値にするのが有効かは実際に入力ノードの形状と大きさに依存するので、事前に最適なサイズを決定するのは難しいと思います
このサイズが性能にクリティカルな影響を与えるわけではないですが、ハードコードよりも環境変数などで外部から注入できるようにしたほうが良いと思いました
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.

「事前に確保するコードがあることで悪い影響が起きる可能性」というのは、

  • 確保したけれど使わなかったオーバーヘッド
  • 解放時の手間

とかでしょうか?
後者はともかく、前者はlinuxに関しては、アクセスがあるまで仮想アドレスが割り当てられるだけで実際に物理メモリが割り当てられるわけではないからそれほど害はないと感じています。
考慮しきれていないものがあったら教えてください。

Comment on lines +61 to +64
def get_val(node: Optional[TreeNode]) -> int:
if node is None:
return 0
return node.val
Copy link

Choose a reason for hiding this comment

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

このあたりのアクセッサーが冗長かなと思います。ここらへん減らしたかったら、番兵を立てるのも手です。

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.cxy3cik6kyqx

Copy link
Owner Author

Choose a reason for hiding this comment

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

これならば、root1, 2がNoneかどうかで4通りすべて書く必要がなくなりますね。ありがとうございます。

root = TreeNode()
stack = [(root, roots)]
while stack:
dst_node, src_nodes = stack.pop()
Copy link

@olsen-blue olsen-blue Mar 1, 2025

Choose a reason for hiding this comment

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

filterは初めてみましたが、None除去したりは便利だと感じました。
https://docs.python.org/3/library/functions.html#filter

dst は、 src と対比されていそうですが、これから作りたい destination node みたいなイメージですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうなりますね(完全にodaさんのものの写経になりますが)。

ループの先頭時点で、dst_nodeはTreeNode()となっているが値や子ノードが適切に設定されていない、src_nodesはdst_nodeと位置的に対応するnodeとなっています。ループないでvalやleft, rightを決めていくことになります

merged_val = get_val(root1) + get_val(root2)
merged_root = TreeNode(val=merged_val)
merged_root.left = self.mergeTrees(get_left(root1), get_left(root2))
merged_root.right = self.mergeTrees(
Copy link

Choose a reason for hiding this comment

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

一貫性があると読みやすくなるので、改行するしないをleftと合わせるほうが良いと思いました。

if node is None:
return None
return node.left
def get_right(node: Optional[TreeNode]) -> Optional[TreeNode]:
Copy link

@colorbox colorbox Mar 1, 2025

Choose a reason for hiding this comment

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

nodeがnoneだったらnoneを返して、そうでなければleft/rightを返す、これってメソッドの呼び出し元でnode.left/node.rightを呼び出すのと何も違いがなく見えます。
このメソッドはなくしてしまったほうが読みやすくなるように思えます。
すみません、読み間違えました。
ただ、メソッドをなくしたほうがよみやすそうという感覚は変わらずでして、nodeの存在確認は
if node1_left is not None or node2_left is not 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.

4 participants