-
Notifications
You must be signed in to change notification settings - Fork 0
226. Invert Binary Tree #71
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
base: main
Are you sure you want to change the base?
Conversation
| return nullptr; | ||
| } | ||
|
|
||
| TreeNode* inverted_left = invertTree(node->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.
std::swap(node->left, node->right);
node->left = invertTree(node->left);
node->right = invertTree(node->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.
@nodchip
レビューありがとうございます。step2_3に追加しました。
| class Solution { | ||
| public: | ||
| TreeNode* invertTree(TreeNode* root) { | ||
| if (!root) { |
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.
個人的には脳内フローチャートをシンプルにするために、この条件分岐は入れませんね、rootがnullの場合もline 24で回収されるので。ただ、コーナーケース?を先に回収しておく派の人もいると思うので、個人的な好みかと思います。
参考までに、関連しそうなコメントを最近見かけたので残しておきます。
https://github.com/sota009/swe-coding-practice/pull/1/files#r2079144520
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.
@huyfififi
レビューありがとうございます。いつもは先に回収してしまいたい派ですが、左右のノードは取り出した後にnullptr判定しているので統一感がなかったです。。。step4で指摘いただきました箇所は削除しました。
| if (!root) { | ||
| return nullptr; | ||
| } | ||
| queue<TreeNode*> traversing_nodes; |
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.
私はC++に疎いので変なことを言っている可能性が高いですが...LeetCode内でusing namespace std;が補足されているとは思いますが、#include <queue>を回答内で明示してstd::queueとした方が、実務を意識していて名前空間を汚すことに抵抗を持っていますアピールになるかもしれません。
参考までに、関連しそうなコメントが記憶の片隅にあったので置いておきます。
irohafternoon/LeetCode#8 (comment)
Google Style Guide - Namespaces
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.
@huyfififi
ありがとうございます。
コーディング面接の際には、前置きをした上でstd::等を省くつもりでした。が、練習の場ですのでつけているバージョンも書いておくべきでした。同じくstep4に追加しました。
実務ではないですがこちらの会の将棋AI開発をC++で挑戦しましたが名前空間を汚すことで何度かハマりました。。。
問題へのリンク
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内に各ステップで感じたことを追記します。