Conversation
kazukiii
reviewed
Jul 19, 2024
Comment on lines
+20
to
+21
| if not root1 and not root2: | ||
| return None |
|
|
||
| if not node1 and not node2: | ||
| continue | ||
| if node1 and not node2: |
Owner
Author
There was a problem hiding this comment.
ここは冗長ですが、二つのnodeの状態がパッとみて分かるように敢えてこうしました。
fhiyo
reviewed
Jul 20, 2024
| # self.right = right | ||
| class Solution: | ||
| def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
| if not root1 or not root2: |
There was a problem hiding this comment.
好みだと思いますが、 if not (root1 and root2) の方が個人的には分かりやすいと思いました。
root1とroot2の両方があるわけじゃなければ、root1のコピーかroot2のコピーを返す。
Owner
Author
There was a problem hiding this comment.
ドモルガンですね。私も実はこっちを先に考えて反転させました(コメントを見ていると()をあまり使わない方が好まれるような気がしたので)
hayashi-ay
reviewed
Jul 20, 2024
| # self.right = right | ||
| class Solution: | ||
| def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: | ||
| if not root1 or not root2: |
Owner
Author
There was a problem hiding this comment.
有難うございます。
ここは過去ログを見ると、どちらを採用するか派閥が割れてそうですね(読みやすさの観点ではご指摘の通りだと思います)
There was a problem hiding this comment.
これ、
return deepcopy(root2) or deepcopy(root1)
が分かりにくくしている気がしますね。
|
良いと思います。if文をどう書くかは他のlogを見る限り好みの範疇なのかなと思いました |
oda
reviewed
Jul 21, 2024
| continue | ||
|
|
||
| merged_node.left = merge_nodes(node1.left, node2.left) | ||
| merged_node.right = merge_nodes(node1.right, node2.right) |
There was a problem hiding this comment.
スタックを積んでいる意味がないということです。
ノードを作る部分を親に押し付ける、みたいなことをする必要があります。
Owner
Author
There was a problem hiding this comment.
書き直してみました。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return None
merged_root = TreeNode()
next_nodes = [(root1, root2, merged_root)]
while next_nodes:
node1, node2, merged_node = next_nodes.pop()
if not node1 and not node2:
continue
if not node1 and node2:
merged_node.val = node2.val
if node2.right:
merged_node.right = TreeNode(None)
next_nodes.append((None, node2.right, merged_node.right))
if node2.left:
merged_node.left = TreeNode(None)
next_nodes.append((None, node2.left, merged_node.left))
continue
if node1 and not node2:
merged_node.val = node1.val
if node1.right:
merged_node.right = TreeNode(None)
next_nodes.append((node1.right, None, merged_node.right))
if node1.left:
merged_node.left = TreeNode(None)
next_nodes.append((node1.left, None, merged_node.left))
continue
merged_node.val = node1.val + node2.val
if node1.right or node2.right:
merged_node.right = TreeNode(None)
next_nodes.append((node1.right, node2.right, merged_node.right))
if node1.left or node2.left:
merged_node.left = TreeNode(None)
next_nodes.append((node1.left, node2.left, merged_node.left))
return merged_root
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/merge-two-binary-trees/description/