Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions arai60/merge_two_binary_trees/phase1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 28分, merge方法に苦しんだ末にどうにか思いつく
# root1にroot2を統合するイメージ
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 is None and root2 is None:
return
elif root1 is None:
root1 = TreeNode()
elif root2 is None:
root2 = TreeNode()
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1
40 changes: 40 additions & 0 deletions arai60/merge_two_binary_trees/phase2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Reference
fhiyo: https://github.com/fhiyo/leetcode/pull/25/files if not の場合早期returnすることができる(新しいのを作らなくても済むのでGood)
sakupan: https://github.com/sakupan102/arai60-practice/pull/24/files
hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/12/files

>>>
新しいのを作るか作らないのか、古い入力を壊すのか壊さないのか、共有するのかしないのか(変更しない前提ならばメモリー使用量が減る)、などのオプションがあって、自分がどれを「選択」したかを意識しましょう。

入力を破壊している。
https://discord.com/channels/1084280443945353267/1252267683731345438/1252556045524537344
出力を変更されるとキャッシュが変わって次からの呼び出しが狂う。
https://discord.com/channels/1084280443945353267/1252267683731345438/1252591437485441024

phase1ではroot1のメモリ領域を使うイメージで作った。新しくTreeを作るイメージの時はまた別の書き方の方が良い
"""

# root1に統合
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:

Choose a reason for hiding this comment

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

全体的に良いと思いました。
どちらかがNoneの場合root1 or root2でreturnしても良いかもしれません。

return root2
elif not root2:
return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1

# 新しくtreeを生成
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:
return copy.deepcopy(root2)
elif not root2:
return copy.deepcopy(root1)
merged_tree = TreeNode(root1.val + root2.val)
merged_tree.left = self.mergeTrees(root1.left, root2.left)
merged_tree.right = self.mergeTrees(root1.right, root2.right)
return merged_tree
10 changes: 10 additions & 0 deletions arai60/merge_two_binary_trees/phase3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:
return root2
elif not root2:
Copy link

Choose a reason for hiding this comment

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

elifじゃなくてifでよいと思います

Copy link

Choose a reason for hiding this comment

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

そうですね。elif は、結構強い意図を感じます。

if A:
  B
elif C:
  D
E

とあったときに、B の継続が E であることを強く主張しているように見えます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

elifってそんな印象になってしまうんですね, ありがとうございます

return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1

Choose a reason for hiding this comment

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

全体的に良いと思いました。dequeを使ってBFSでもかけるので、書いてみても良いと思います。