Skip to content

103. Binary Tree Zigzag Level Order Traversal#26

Open
hemispherium wants to merge 1 commit into
mainfrom
0103-binary-tree-zigzag-level-order-traversal
Open

103. Binary Tree Zigzag Level Order Traversal#26
hemispherium wants to merge 1 commit into
mainfrom
0103-binary-tree-zigzag-level-order-traversal

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 20, 2026
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> result;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

result が空であることを利用している点が、ややパズルに感じました。 initializer list を使って以下のように書いたほうがシンプルだと感じました。

if (!root) return {};

vector<vector<int>> result;

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.

ありがとうございます。initializer listを使った書き方は知りませんでした。

public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> result;
if (!root) return result;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
Shunii85/arai60#17 (comment)

queue<TreeNode*> nodes;
nodes.push(root);

bool toggle = false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

toggle という変数名に、あまり情報がないように感じました。 right_to_left などはいかがでしょうか?

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.

そうですね、right_to_leftの方がtrue, falseの意味も分かりやすくていい気がします。

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.

2 participants