-
Notifications
You must be signed in to change notification settings - Fork 0
103. Binary Tree Zigzag Level Order Traversal #28
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
Open
kazukiii
wants to merge
1
commit into
main
Choose a base branch
from
arai60/binary-tree-zigzag-level-order-traversal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| ## 考察 | ||
| - 初見の問題 | ||
| - 方針 | ||
| - BFS | ||
| - レベルごとに探索して、levelを0始まりで考えたときに奇数なら逆順にして答えに追加すれば良さそう | ||
| - 各reverse処理の計算量は要素数オーダーかかるが、全体としてはノード数で抑えられるので、amortized O(1) | ||
| - time: O(n), space: O(n) | ||
| - DFS | ||
| - DFSでも出来るが、前回同様DFSで解くメリットは特にない | ||
| - DFSは前回書いたので今回はスキップ | ||
| - BFSで、vectorを2つ用意する形で実装する | ||
|
|
||
| ## Step1 | ||
| - 他の人のPRを検索 | ||
| - https://github.com/fhiyo/leetcode/pull/29 | ||
| - 文字通りzigzagにtraverseしている | ||
| - おそらくこれが想定解 | ||
| - どうやって考えたら思い付けるか落とし込むことが出来なかったので、一旦覚える | ||
| - from_leftを用意している | ||
| - levelの偶奇で判定するより可読性が高そう | ||
| - https://github.com/YukiMichishita/LeetCode/pull/11 | ||
| - reverseしなくてもdequeを使えば、逆順になる | ||
| - https://github.com/sakupan102/arai60-practice/pull/28 | ||
| - `step2_zigzag_traverse.cpp` と `spte2_deque.cpp` を追加 | ||
|
|
||
| ## Step3 | ||
| - 1回目: 3m52s | ||
| - 2回目: 3m20s | ||
| - 3回目: 3m25s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * 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) { | ||
| if (!root) return {}; | ||
| vector<TreeNode*> nodes_to_visit; | ||
| nodes_to_visit.push_back(root); | ||
| vector<vector<int>> answer; | ||
| int level = 0; | ||
| while (!nodes_to_visit.empty()) { | ||
| vector<int> values_in_level; | ||
| vector<TreeNode*> nodes_in_next_level; | ||
| for (int i = 0; i < nodes_to_visit.size(); i++) { | ||
| TreeNode* node = nodes_to_visit[i]; | ||
| values_in_level.push_back(node->val); | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| } | ||
| if (level % 2 == 1) reverse(values_in_level.begin(), values_in_level.end()); | ||
| answer.push_back(move(values_in_level)); | ||
| swap(nodes_to_visit, nodes_in_next_level); | ||
| level++; | ||
| } | ||
| return answer; | ||
| } | ||
| }; |
39 changes: 39 additions & 0 deletions
39
arai60/binary-tree-zigzag-level-order-traversal/step2_deque.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * 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) { | ||
| if (!root) return {}; | ||
| vector<TreeNode*> nodes_to_visit; | ||
| nodes_to_visit.push_back(root); | ||
| vector<vector<int>> answer; | ||
| int level = 0; | ||
| while (!nodes_to_visit.empty()) { | ||
| deque<int> values_in_level; | ||
| vector<TreeNode*> nodes_in_next_level; | ||
| for (int i = 0; i < nodes_to_visit.size(); i++) { | ||
| TreeNode* node = nodes_to_visit[i]; | ||
| if (level % 2 == 0) { | ||
| values_in_level.push_back(node->val); | ||
| } else { | ||
| values_in_level.push_front(node->val); | ||
| } | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| } | ||
| answer.emplace_back(values_in_level.begin(), values_in_level.end()); | ||
| swap(nodes_to_visit, nodes_in_next_level); | ||
| level++; | ||
| } | ||
| return answer; | ||
| } | ||
| }; |
39 changes: 39 additions & 0 deletions
39
arai60/binary-tree-zigzag-level-order-traversal/step2_zigzag_traverse.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * 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) { | ||
| if (!root) return {}; | ||
| vector<TreeNode*> nodes; | ||
| nodes.push_back(root); | ||
| bool left_to_right = true; | ||
| vector<vector<int>> answer; | ||
| while (!nodes.empty()) { | ||
| auto& values_in_level = answer.emplace_back(); | ||
| vector<TreeNode*> nodes_in_next_level; | ||
| for (int i = nodes.size() - 1; i >= 0; i--) { | ||
| TreeNode* node = nodes[i]; | ||
| values_in_level.push_back(node->val); | ||
| if (left_to_right) { | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| } else { | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| } | ||
| } | ||
| swap(nodes, nodes_in_next_level); | ||
| left_to_right = !left_to_right; | ||
| } | ||
| return answer; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * 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) { | ||
| if (!root) return {}; | ||
| vector<TreeNode*> nodes; | ||
| nodes.push_back(root); | ||
| bool left_to_right = true; | ||
| vector<vector<int>> answer; | ||
| while (!nodes.empty()) { | ||
| auto& values_in_level = answer.emplace_back(); | ||
| vector<TreeNode*> nodes_in_next_level; | ||
| for (int i = nodes.size() - 1; i >= 0; i--) { | ||
| TreeNode* node = nodes[i]; | ||
| values_in_level.push_back(node->val); | ||
| if (left_to_right) { | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| } else { | ||
| if (node->right) nodes_in_next_level.push_back(node->right); | ||
| if (node->left) nodes_in_next_level.push_back(node->left); | ||
| } | ||
| } | ||
| nodes = move(nodes_in_next_level); | ||
| left_to_right = !left_to_right; | ||
| } | ||
| return answer; | ||
| } | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
あえてvectorをstackっぽく使っているように見えた (LIFOの順に要素を見ているので) んですが、stackでなくvectorを使った理由ってありますか?
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.
いつもありがとうございます。
強いこだわりがある訳ではないですが、以前頂いたこちらのコメントを参考に、今回の場合も順不同で push, pop する訳ではないため vector を使っています。#27 (comment)