Skip to content

617. Merge Two Binary Trees#12

Open
hayashi-ay wants to merge 6 commits intomainfrom
hayashi-ay-patch-1
Open

617. Merge Two Binary Trees#12
hayashi-ay wants to merge 6 commits intomainfrom
hayashi-ay-patch-1

Conversation

@hayashi-ay
Copy link
Copy Markdown
Owner

Comment on lines +46 to +47
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.

この条件は不要ではないでしょうか。
両方Noneのときは、

if not root1:
            return 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.

たしかにそうですね。ありがとうございます。

Comment on lines +52 to +54
new_node = TreeNode(root1.val + root2.val)
new_node.left = self.mergeTrees(root1.left, root2.left)
new_node.right = self.mergeTrees(root1.right, root2.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
Owner Author

Choose a reason for hiding this comment

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

問題文的には既存の木に足していく方が自然かもですね。

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.

2 participants