617. Merge Two Binary Trees#23
Open
hiroki-horiguchi-dev wants to merge 1 commit into
Open
Conversation
h-masder
reviewed
Jun 3, 2026
| - 時間計算量, 空間計算量: O(N) root1, root2 の大きい方に依存する | ||
| - スタックオーバーフローが発生する可能性を考え始めないといけない | ||
| - Queue | ||
| - root1, root2 で和をとるべき相対位置はQueueを使うと管理しやすいなということで、パッと思いついたが、じゃあノードの張り替えをどうするか?で完全に詰まってしまった |
There was a problem hiding this comment.
せっかくなので再度トライしてみて下さい。
入力の木を変更しないためには、新しい木を作ればよいですね。
例えば次のように場合分けする良いと思います。
あるノードを作るときに、
1)両方の木にノードがある場合:値を足し合わせたノードを作る
(これは、載せていただいたコードを見る限り、すでにできると思います。)
2)片方の木にしかノードがない場合:存在しない側を 0 とみなし、(存在する側の値+0)を値に持つノードを作る
3)両方とも null の場合: 何もしない
Owner
Author
There was a problem hiding this comment.
ですね、再度トライします。
新しい木を作る時に、新しい木の先頭をどこに持っておくかが、パッと思いつかなくて再帰でいいやとなっています。
There was a problem hiding this comment.
それでしたら、以下のPRを参考にしてみてください。
https://github.com/seal-azarashi/leetcode/pull/22/changes
また、コードだけでなくPRの中のコメントについても考えてみてもいいかもしれません。
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.
617. Merge Two Binary Treesを解きました。