-
Notifications
You must be signed in to change notification settings - Fork 0
Merge two binary trees #35
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: arai60
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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: | ||
| 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 | ||
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. elifじゃなくてifでよいと思います There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね。elif は、結構強い意図を感じます。 if A:
B
elif C:
D
Eとあったときに、B の継続が E であることを強く主張しているように見えます。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 全体的に良いと思いました。dequeを使ってBFSでもかけるので、書いてみても良いと思います。 |
||
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の場合
root1 or root2でreturnしても良いかもしれません。