Skip to content

103. Binary Tree Zigzag Level Order Traversal#29

Merged
Ryotaro25 merged 6 commits intomainfrom
problem27
Jul 2, 2025
Merged

103. Binary Tree Zigzag Level Order Traversal#29
Ryotaro25 merged 6 commits intomainfrom
problem27

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/

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

備考

次に解く問題の予告
Validate Binary Search Tree

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

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

if (level % 2 == 0) {
values.emplace_back(node->val);
} else {
auto it = values.begin();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数を作らなくて良さそうかと思います

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.

@fhiyo
レビューありがとうございます。
確かに不要でしたstep1_2.cppにて修正しました。

Comment on lines +41 to +42
current_nodes = next_level_nodes;
level_to_zigzag_values.emplace_back(values);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここと同じですね。詳しくないので自信はないですが <utility>ヘッダにある std::moveを使えば良いかもしれないです

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.

@fhiyo
102で指摘されてからこの辺り調べ直しましした。
また、step1_2とstep3_2にstd::moveを使ったバージョンを作成しました🙇
ありがとうございます。

values.emplace_back(node->val);
} else {
auto it = values.begin();
values.insert(it, node->val);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これがあるから時間計算量O((ノード数)^2)になりますかね?

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.

@fhiyo
指摘いただいた通りだと思います。見落としておりました。。。

class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> level_to_zigzag_values = {};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

level_to_valuesで十分かなと思います

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.

@Yoshiki-Iwasa
いつもレビューありがとうございます。
修正しました!

if (!root) {
return level_to_zigzag_values;
}
vector<TreeNode*> current_nodes = {root};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

フォーマット規則では中括弧にスペースいらないですか??

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.

@Yoshiki-Iwasa
意識しておりませんでした。ありがとうございます!!
下記の通りでした。

変数初期化時には不要のようです。
https://google.github.io/styleguide/cppguide.html#Variable_and_Array_Initialization

if (x == kFoo) { return new Foo(); }のように一行で書く場合は、スペースが必要なようです。
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

reverse(values.begin(), values.end());
}
level++;
current_nodes = next_level_nodes;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

copy assignmentが呼ばれるんじゃないでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

swap か move でしょうね。

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.

@fhiyo @oda
ご指摘の通りです。move周り慣れるまでは特に注意したいと思います。

Comment on lines +30 to +33
next_level_nodes.emplace_back(std::move(node->left));
}
if (node->right) {
next_level_nodes.emplace_back(std::move(node->right));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ポインタに対してmoveをするのってどういうメリットがあるんでしょうか?

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.

@fhiyo
レビューありがとうございます。意味ないですね。。。
アドレス値を持っているだけなのでコピーしてもmoveしても変わらないです。
気をつけます。

修正しました🙇

@TORUS0818
Copy link
Copy Markdown

拝見しました。
コメント以外は良いと思いました。

@Ryotaro25 Ryotaro25 merged commit 3ff55d3 into main Jul 2, 2025
1 check passed
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.

5 participants