-
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?
Conversation
fhiyo
left a comment
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.
良いと思います!
| 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。elif は、結構強い意図を感じます。
if A:
B
elif C:
D
Eとあったときに、B の継続が E であることを強く主張しているように見えます。
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.
elifってそんな印象になってしまうんですね, ありがとうございます
| # root1に統合 | ||
| class Solution: | ||
| def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
| if not root1: |
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しても良いかもしれません。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
全体的に良いと思いました。dequeを使ってBFSでもかけるので、書いてみても良いと思います。
問題
617. Merge Two Binary Trees