Skip to content

617.Merge-Two-Binary-Trees#5

Open
PafsCocotte wants to merge 2 commits intomainfrom
617.Merge-Two-Binary-Trees
Open

617.Merge-Two-Binary-Trees#5
PafsCocotte wants to merge 2 commits intomainfrom
617.Merge-Two-Binary-Trees

Conversation

@PafsCocotte
Copy link
Copy Markdown
Owner

@PafsCocotte PafsCocotte commented Feb 15, 2026

三項演算子を多用したAIの回答が読みやすかったです。
三項演算子を見ていると、やっぱりifは文ではなく式だよなって思いました。(ifを式とするrustからは、コンパイル通せないので目をそらしつつ)

t1がnullptrならt2を直接渡してしまえばメモリ節約できるともいわれたが、deleteが大変すぎるので採用しない。
他の方を見たらbfsや親から子に伝搬する形で書く人が多いか
https://discord.com/channels/1084280443945353267/1295357747545505833/1329746604114055191
はtreeがn個になったときに綺麗だが、cppらしい書き方はわからない。lambdaとかか?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

reference が取れるのでもっと簡単に書けるはずです。
std::pair<TreeNode**, std::vector<TreeNode*>> をスタックに積んでみましょう。
また、メンバーポインターという面白い文法があったりします。
TreeNode* TreeNode::* branches[] = { &TreeNode::left, &TreeNode::right };

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.

まずメンバポインタを知りませんでした。
勉強なので両方書いてみます、ありがとうございます!

@PafsCocotte PafsCocotte changed the title step1,2 617.Merge-Two-Binary-Trees Feb 16, 2026
root1あるいはroot2のどっちかにノードがあるならノードを作成して値を入れる というのがやること。
今回作る関数はheadを返すだけなのでdeleteは関数ユーザーの責任。
nullptr参照に気を付けるべき。とくにroot1とroot2の深さが2以上違う場合。
leetcodeの引数名をleftRoot, rightRootにする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これが leftLeft のような分かりにくい変数名の原因になっています。もとのシグネチャならそれほど気にならないように思いました(それでも left1 のようなベタ書き感はあるんですが)。

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