Skip to content

617. Merge Two Binary Trees#25

Open
TORUS0818 wants to merge 1 commit intomainfrom
617
Open

617. Merge Two Binary Trees#25
TORUS0818 wants to merge 1 commit intomainfrom
617

Conversation

@TORUS0818
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@kazukiii kazukiii left a comment

Choose a reason for hiding this comment

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

全体的に良さそうに思います。

Comment on lines +20 to +21
if not root1 and not root2:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あった方がわかりやすいかもですが、一応この2行下で吸収できますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。思考から漏れてました。


if not node1 and not node2:
continue
if node1 and not node2:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題ですが、if not node2: でも良さそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ここは冗長ですが、二つのnodeの状態がパッとみて分かるように敢えてこうしました。

Copy link
Copy Markdown

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

良いと思います!

# self.right = right
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 or not root2:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みだと思いますが、 if not (root1 and root2) の方が個人的には分かりやすいと思いました。
root1とroot2の両方があるわけじゃなければ、root1のコピーかroot2のコピーを返す。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ドモルガンですね。私も実はこっちを先に考えて反転させました(コメントを見ていると()をあまり使わない方が好まれるような気がしたので)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は or 派ですね。趣味の範囲では。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います。

# self.right = right
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 or not root2:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if文を2つに分けたほうが読みやすいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。
ここは過去ログを見ると、どちらを採用するか派閥が割れてそうですね(読みやすさの観点ではご指摘の通りだと思います)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、
return deepcopy(root2) or deepcopy(root1)
が分かりにくくしている気がしますね。

@SuperHotDogCat
Copy link
Copy Markdown

良いと思います。if文をどう書くかは他のlogを見る限り好みの範疇なのかなと思いました

continue

merged_node.left = merge_nodes(node1.left, node2.left)
merged_node.right = merge_nodes(node1.right, node2.right)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここで再帰していたらあまり意味がないのでは?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スタックを積んでいる意味がないということです。
ノードを作る部分を親に押し付ける、みたいなことをする必要があります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かにです、、ちょっと考えてみます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

書き直してみました。

# 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants