Skip to content
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

617. Merge Two Binary Trees #26

Merged
merged 3 commits into from
Mar 8, 2025
Merged

617. Merge Two Binary Trees #26

merged 3 commits into from
Mar 8, 2025

Conversation

ryoooooory
Copy link
Owner

@ryoooooory ryoooooory commented Mar 4, 2025

if (root1 == null && root2 == null) {
return null;
}
// 配列でやってるが、構造体を用意してもいい。
Copy link

Choose a reason for hiding this comment

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

Java の場合、定義するほうが趣味ですね。同じものが繰り返されているわけではないので。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!定義する方が明示的ですしそちらを意識するようにします

TreeNode merge = nodes[0];
TreeNode left = nodes[1];
TreeNode right = nodes[2];

Copy link

Choose a reason for hiding this comment

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

少し場合分けが多いですね。番兵を使うのは一つでしょう。
{0, null, null} を置いておきます。

Copy link

Choose a reason for hiding this comment

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

Copy link
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 +16 to +22
if (root1 == null) {
mergedTree.val = root2.val;
} else if (root2 == null) {
mergedTree.val = root1.val;
} else {
mergedTree.val = root1.val + root2.val;
}
Copy link

@colorbox colorbox Mar 6, 2025

Choose a reason for hiding this comment

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

root1があったらroot1.valを加算
root2があったらroot2.valを加算
とすることでこのブロック全体をシンプルに読みやすくできそうに思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!条件分と処理が反転するよりそちらのがたしかに直感的でわかりやすいですね!

Copy link
Owner Author

Choose a reason for hiding this comment

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

あとから気づいたんですが、最初のこの処理はwhileでの処理と重複するのでいらなそうですね、、、

TreeNode[] nodes = nodesForMerge.pop();
TreeNode merge = nodes[0];
TreeNode left = nodes[1];
TreeNode right = nodes[2];
Copy link

Choose a reason for hiding this comment

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

left,rightがTreeNodeのメンバとおなじ名前で強めの違和感を感じました。
node1,node2等といった名前にするのはいかがでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!おっしゃるとおり読み返してもわかりにくいのでそちらのleft,rightはやめようとおもいます

Comment on lines +32 to +50
if (left == null) {
merge.val = right.val;
if (right.left != null) {
merge.left = nextMergedLeftNode;
nodesForMerge.push(new TreeNode[] {nextMergedLeftNode, null, right.left});
}
} else if (right == null) {
merge.val = left.val;
if (left.left != null) {
merge.left = nextMergedLeftNode;
nodesForMerge.push(new TreeNode[] {nextMergedLeftNode, left.left, null});
}
} else {
merge.val = left.val + right.val;
if (left.left != null || right.left != null) {
merge.left = nextMergedLeftNode;
nodesForMerge.push(new TreeNode[] {nextMergedLeftNode, left.left, right.left});
}
}
Copy link

Choose a reason for hiding this comment

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

rightが存在するときはmerge.valにright.valを加算、
leftが存在するときはmerge.valにleft.valを加算、
というふうに書くと冗長なif分岐を消せてコードがシンプルになり、読みやすくなりそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!

nodesForMerge.push(new TreeNode[] {nextMergedRightNode, left.right, right.right});
merge.right = nextMergedRightNode;
}
}
Copy link

@colorbox colorbox Mar 6, 2025

Choose a reason for hiding this comment

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

具体的な解決案を示せず申し訳ないのですが
left/rightの違いしかないほぼ同じことの繰り返しなところに違和感を感じました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!上記のコメントでいただいた番兵をつかうとかなり繰り返しをけせました!

@ryoooooory ryoooooory merged commit d39635c into main Mar 8, 2025
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.

3 participants