-
Notifications
You must be signed in to change notification settings - Fork 0
617. Merge Two Trees #30
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
base: main
Are you sure you want to change the base?
Conversation
https://leetcode.com/problems/merge-two-binary-trees/description/ You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. Return the merged tree. Note: The merging process must start from the root nodes of both trees.
- returnする値が多くて見にくい気も | ||
- このときはdeepcopyしてrootをreturnする選択肢に気づいていない | ||
- merged_rootとmerged_nodeで変数名を迷う(再帰をしていく中でこの変数はrootでなくなるので) | ||
|
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.
これ確かに自分抜けがちです。ありがとうございます
- よって、引き継ぎで必要な情報は、今のmerged_nodeと、leftのmerged_nodeと、rightのmerged_node。(行きには現在のnodeが必要) | ||
- appendする時には、mutableなlistにしてbackのmerged_leftとgoのcurrent_merged(=次に取り出されるbackのmerge)が同じものを指すようにする | ||
- listがその要素への参照なので、1要素のlistがCとかの参照の代わりになるというの、確かに | ||
- めっちゃ時間かかった、以前1回実装を読んだつもりだったが、ちゃんと理解できてなかったことが判明、、、 |
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.
Python でのこれは半分概念の説明のためのもので実用性はあまりないですね。
|
||
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
def merge_helper(node1, node2, merged): | ||
if not(node1.left or node2.left or node1.right or node2.right): |
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.
細かいですがkeywordであるnotが関数のように見えるのでnotと()の間は空白を入れた方が良い気がします。
(実際は関数名と括弧の間は空白入れられるようではありますが...)
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.
この場合はドモルガンで展開した方が分かりやすそうですかね。
また、このifは不要に思えます。
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.
ありがとうございます。
not のようなキーワードの場合空白入れるんですね。確かにandでもよかったかもですね。
確かにearly returnを意識しすぎましたが不要な条件分岐でしたね。
class Solution: | ||
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
# node.valと、node.leftと、node.rightについて、nodeがNoneの場合でも共通して処理できるようにする | ||
def process_node_when_not_existing(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.
Noneをチェックするだけなので、processという動詞と合っていないように思いました。
node_val = node.val if node else 0 | ||
node_left = node.left if node else None | ||
node_right = node.right if node else None | ||
return node_val, node_left, node_right |
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.
何度もinline ifを使っているのが気になります。
if not node:
return 0, None, None
return node.val, node.left, node.right
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.
ありがとうございます。確かにそれでいいですね
https://github.com/TORUS0818/leetcode/pull/25 | ||
|
||
- どちらかなくなったらもう一方をそのまま返せば良いと言うことになぜか気づかなかった… | ||
- 非破壊的にしたければdeepcopy()か |
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.
インプットのツリーは参照されているものの、破壊はされていないです。https://en.wikipedia.org/wiki/Copy-on-write
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とかが後で変更されると、すでに実行した関数の返り値も変更されてしまうということだったんですね。
ありがとうございます。
|
||
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
def merge_helper(node1, node2, merged): | ||
if not(node1.left or node2.left or node1.right or node2.right): |
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.
この場合はドモルガンで展開した方が分かりやすそうですかね。
また、このifは不要に思えます。
if node1.left or node2.left: | ||
node1_left = self.make_dummy_node_when_none(node1.left) | ||
node2_left = self.make_dummy_node_when_none(node2.left) | ||
merged.left = TreeNode(node1_left.val + node2_left.val) | ||
merge_helper(node1_left, node2_left, merged.left) | ||
if node1.right or node2.right: | ||
node1_right = self.make_dummy_node_when_none(node1.right) | ||
node2_right = self.make_dummy_node_when_none(node2.right) | ||
merged.right = TreeNode(node1_right.val + node2_right.val) | ||
merge_helper(node1_right, node2_right, merged.right) |
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.
deepcopyが使えそうですか。
if node1.left and node2.left:
merged.left = TreeNode(node1.left.val + node2.left.val)
merge_helper(node1.left, node2.left, merged.left)
else:
merged.left = deepcopy(node1.left if node1.left else node2.left)
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.
なるほど、これは思いついてなかったです、ありがとうございます
|
||
https://github.com/TORUS0818/leetcode/pull/25 | ||
|
||
- どちらかなくなったらもう一方をそのまま返せば良いと言うことになぜか気づかなかった… |
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.
最初からこれに気づくのは難しいですね。
https://leetcode.com/problems/merge-two-binary-trees/description/ You are given two binary trees root1 and root2.
Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
Return the merged tree.
Note: The merging process must start from the root nodes of both trees.