Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/invert-binary-tree/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
https://leetcode.com/problems/maximum-product-subarray/description/

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

return nullptr;
}

TreeNode* inverted_left = invertTree(node->left);
Copy link

Choose a reason for hiding this comment

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

std::swap(node->left, node->right);
node->left = invertTree(node->left);
node->right = invertTree(node->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.

@nodchip
レビューありがとうございます。step2_3に追加しました。

class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (!root) {

Choose a reason for hiding this comment

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

個人的には脳内フローチャートをシンプルにするために、この条件分岐は入れませんね、rootがnullの場合もline 24で回収されるので。ただ、コーナーケース?を先に回収しておく派の人もいると思うので、個人的な好みかと思います。

参考までに、関連しそうなコメントを最近見かけたので残しておきます。
https://github.com/sota009/swe-coding-practice/pull/1/files#r2079144520

Copy link
Owner Author

Choose a reason for hiding this comment

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

@huyfififi
レビューありがとうございます。いつもは先に回収してしまいたい派ですが、左右のノードは取り出した後にnullptr判定しているので統一感がなかったです。。。step4で指摘いただきました箇所は削除しました。

if (!root) {
return nullptr;
}
queue<TreeNode*> traversing_nodes;
Copy link

@huyfififi huyfififi Jun 16, 2025

Choose a reason for hiding this comment

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

私はC++に疎いので変なことを言っている可能性が高いですが...LeetCode内でusing namespace std;が補足されているとは思いますが、#include <queue>を回答内で明示してstd::queueとした方が、実務を意識していて名前空間を汚すことに抵抗を持っていますアピールになるかもしれません。

参考までに、関連しそうなコメントが記憶の片隅にあったので置いておきます。
irohafternoon/LeetCode#8 (comment)
Google Style Guide - Namespaces

Copy link
Owner Author

Choose a reason for hiding this comment

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

@huyfififi
ありがとうございます。
コーディング面接の際には、前置きをした上でstd::等を省くつもりでした。が、練習の場ですのでつけているバージョンも書いておくべきでした。同じくstep4に追加しました。

実務ではないですがこちらの会の将棋AI開発をC++で挑戦しましたが名前空間を汚すことで何度かハマりました。。。

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.

4 participants