617.merge two binary trees#22
Open
nicah4o wants to merge 1 commit into
Open
Conversation
liquo-rice
reviewed
May 28, 2026
|
|
||
| BFSのこの方法を写させてもらった。 | ||
| 番兵を使うことでnullptrのleftなどへのアクセスを可能にしているほか、TreeNodeのポインタのポインタを使用することで参照渡しを可能にしている。 | ||
| (キューだとFIFOなので上から新しい木を作ることになるが、単純にTreeNode *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。) |
There was a problem hiding this comment.
単純にTreeNode *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。
TreeNode* nodeという変数には何が格納されていますか?
Owner
Author
There was a problem hiding this comment.
レビューありがとうございます。
nodeにはTreeNode型オブジェクトのポインタが入っています。
この文脈で言えば、親から渡された->leftと->rightのポインタの写しが入っています。->leftの->valをいじることはできますが、->left自体を変更することはできません。
There was a problem hiding this comment.
単純にTreeNode *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。
この文脈で言えば、親から渡された->leftと->rightのポインタの写しが入っています。->leftの->valをいじることはできますが、->left自体を変更することはできません。
違うことを言っているように見えます。
Owner
Author
There was a problem hiding this comment.
返信が遅くなり申し訳ありません。
ポインタの定義は、変数のアドレスを格納する変数です。
この場合node->valへの代入で値を変更できると言っている後者のほうが正しいです。
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.
This problem: https://leetcode.com/problems/merge-two-binary-trees/
Next problem: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/