-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
- https://github.com/hroc135/leetcode/pull/22/files#r1826509019 | ||
- stackに入りうるサイズの概算 | ||
- > 事前に確保する配列の大きさをどの値にするのが有効かは実際に入力ノードの形状と大きさに依存するので、事前に最適なサイズを決定するのは難しいと思います | ||
このサイズが性能にクリティカルな影響を与えるわけではないですが、ハードコードよりも環境変数などで外部から注入できるようにしたほうが良いと思いました |
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.
私は、この確保はあんまり賛成しません。
「配列を事前に確保しておくことで速くなるメリット」「事前に確保するコードがあることで悪い影響が起きる可能性」を考えて、前者がほとんどないと判断します。
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.
「事前に確保するコードがあることで悪い影響が起きる可能性」というのは、
- 確保したけれど使わなかったオーバーヘッド
- 解放時の手間
とかでしょうか?
後者はともかく、前者はlinuxに関しては、アクセスがあるまで仮想アドレスが割り当てられるだけで実際に物理メモリが割り当てられるわけではないからそれほど害はないと感じています。
考慮しきれていないものがあったら教えてください。
def get_val(node: Optional[TreeNode]) -> int: | ||
if node is None: | ||
return 0 | ||
return node.val |
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.
このあたりのアクセッサーが冗長かなと思います。ここらへん減らしたかったら、番兵を立てるのも手です。
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.
これならば、root1, 2がNoneかどうかで4通りすべて書く必要がなくなりますね。ありがとうございます。
root = TreeNode() | ||
stack = [(root, roots)] | ||
while stack: | ||
dst_node, src_nodes = stack.pop() |
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.
filterは初めてみましたが、None除去したりは便利だと感じました。
https://docs.python.org/3/library/functions.html#filter
dst は、 src と対比されていそうですが、これから作りたい destination node みたいなイメージですかね。
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.
そうなりますね(完全に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( |
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と合わせるほうが良いと思いました。
if node is None: | ||
return None | ||
return node.left | ||
def get_right(node: Optional[TreeNode]) -> Optional[TreeNode]: |
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.
nodeがnoneだったらnoneを返して、そうでなければleft/rightを返す、これってメソッドの呼び出し元でnode.left/node.rightを呼び出すのと何も違いがなく見えます。
このメソッドはなくしてしまったほうが読みやすくなるように思えます。
すみません、読み間違えました。
ただ、メソッドをなくしたほうがよみやすそうという感覚は変わらずでして、nodeの存在確認は
if node1_left is not None or node2_left is not None:
の行でやってしまって良いと思いました。
https://leetcode.com/problems/merge-two-binary-trees/description/