-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
if (root1 == null && root2 == null) { | ||
return null; | ||
} | ||
// 配列でやってるが、構造体を用意してもいい。 |
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.
Java の場合、定義するほうが趣味ですね。同じものが繰り返されているわけではないので。
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.
ありがとうございます!定義する方が明示的ですしそちらを意識するようにします
TreeNode merge = nodes[0]; | ||
TreeNode left = nodes[1]; | ||
TreeNode right = nodes[2]; | ||
|
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.
少し場合分けが多いですね。番兵を使うのは一つでしょう。
{0, null, null} を置いておきます。
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.
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.
ありがとうございます!番兵つかったら、処理をかなり簡略化できました!番兵をつかうという意識にまだなれていないので、整理してつかえるようにしようと思います
if (root1 == null) { | ||
mergedTree.val = root2.val; | ||
} else if (root2 == null) { | ||
mergedTree.val = root1.val; | ||
} else { | ||
mergedTree.val = root1.val + root2.val; | ||
} |
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.
root1があったらroot1.valを加算
root2があったらroot2.valを加算
とすることでこのブロック全体をシンプルに読みやすくできそうに思います。
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.
ありがとうございます!条件分と処理が反転するよりそちらのがたしかに直感的でわかりやすいですね!
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.
あとから気づいたんですが、最初のこの処理はwhileでの処理と重複するのでいらなそうですね、、、
TreeNode[] nodes = nodesForMerge.pop(); | ||
TreeNode merge = nodes[0]; | ||
TreeNode left = nodes[1]; | ||
TreeNode right = nodes[2]; |
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.
left,rightがTreeNodeのメンバとおなじ名前で強めの違和感を感じました。
node1,node2等といった名前にするのはいかがでしょうか?
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.
ありがとうございます!おっしゃるとおり読み返してもわかりにくいのでそちらのleft,rightはやめようとおもいます
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}); | ||
} | ||
} |
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.
rightが存在するときはmerge.valにright.valを加算、
leftが存在するときはmerge.valにleft.valを加算、
というふうに書くと冗長な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.
ありがとうございます!
nodesForMerge.push(new TreeNode[] {nextMergedRightNode, left.right, right.right}); | ||
merge.right = nextMergedRightNode; | ||
} | ||
} |
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.
具体的な解決案を示せず申し訳ないのですが
left/rightの違いしかないほぼ同じことの繰り返しなところに違和感を感じました。
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.
ありがとうございます!上記のコメントでいただいた番兵をつかうとかなり繰り返しをけせました!
問題URL:https://leetcode.com/problems/merge-two-binary-trees/description/