-
Notifications
You must be signed in to change notification settings - Fork 0
103. Binary Tree Zigzag Level Order Traversal #26
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| 基本は102. Binary Tree Level Order Traversalと同じで、booleanをもって階層ごとに一つ飛ばしで配列をreverseした。 | ||
|
|
||
| ### step2 | ||
|
|
||
| 変更点なし。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
| vector<vector<int>> result; | ||
| if (!root) return result; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
|
|
||
| queue<TreeNode*> nodes; | ||
| nodes.push(root); | ||
|
|
||
| bool toggle = false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toggle という変数名に、あまり情報がないように感じました。 right_to_left などはいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、right_to_leftの方がtrue, falseの意味も分かりやすくていい気がします。 |
||
| while (!nodes.empty()) { | ||
| int size = nodes.size(); | ||
| vector<int> level; | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode* node = nodes.front(); | ||
| nodes.pop(); | ||
|
|
||
| level.push_back(node->val); | ||
|
|
||
| if (node->left) nodes.push(node->left); | ||
| if (node->right) nodes.push(node->right); | ||
| } | ||
|
|
||
| if (toggle) { | ||
| reverse(level.begin(), level.end()); | ||
| } | ||
|
|
||
| toggle = !toggle; | ||
|
|
||
| result.push_back(level); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
| vector<vector<int>> result; | ||
| if (!root) { | ||
| return result; | ||
| } | ||
|
|
||
| queue<TreeNode*> nodes; | ||
| nodes.push(root); | ||
|
|
||
| bool toggle = false; | ||
| while (!nodes.empty()) { | ||
| int size = nodes.size(); | ||
| vector<int> level; | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode* node = nodes.front(); | ||
| nodes.pop(); | ||
|
|
||
| level.push_back(node->val); | ||
| if (node->left) { | ||
| nodes.push(node->left); | ||
| } | ||
| if (node->right) { | ||
| nodes.push(node->right); | ||
| } | ||
| } | ||
| if (toggle) { | ||
| reverse(level.begin(), level.end()); | ||
| } | ||
| toggle = !toggle; | ||
| result.push_back(level); | ||
| } | ||
| return result; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
| vector<vector<int>> result; | ||
| if (!root) { | ||
| return result; | ||
| } | ||
|
|
||
| queue<TreeNode*> nodes; | ||
| nodes.push(root); | ||
|
|
||
| bool toggle = false; | ||
| while (!nodes.empty()) { | ||
| int size = nodes.size(); | ||
| vector<int> level; | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode* node = nodes.front(); | ||
| nodes.pop(); | ||
|
|
||
| level.push_back(node->val); | ||
| if (node->left) { | ||
| nodes.push(node->left); | ||
| } | ||
| if (node->right) { | ||
| nodes.push(node->right); | ||
| } | ||
| } | ||
| if (toggle) { | ||
| reverse(level.begin(), level.end()); | ||
| } | ||
| toggle = !toggle; | ||
| result.push_back(level); | ||
| } | ||
| return result; | ||
| } | ||
| }; |
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.
result が空であることを利用している点が、ややパズルに感じました。 initializer list を使って以下のように書いたほうがシンプルだと感じました。
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.
ありがとうございます。initializer listを使った書き方は知りませんでした。