-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #31
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,21 @@ | ||
| # 102. Binary Tree Level Order Traversal | ||
|
|
||
| https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * Arai60 で解いた問題 | ||
| * https://github.com/ryosuketc/leetcode_arai60/pull/26/files | ||
| * とりあえず各 level ごとに処理する方法。queue を使おうかと思ったが nodes (current level) と next_nodes の入れ替えだけで pop などするわけではなかったので vector のまま書いた | ||
| * とはいえ久々に解いたので12:00くらいかかっった。ちょっとlevelの区切りをどうやって見つけるんだったか悩んでしまった。 | ||
|
|
||
| ### step2 | ||
|
|
||
| * 一応 queue を使う方法。 | ||
| * あとは上記自分の解答のように (node, level) の tuple を queue に入れていく方法もあるが、level ごとに処理する今回の方法がよりわかりやすいとは思う。 | ||
|
|
||
| ### step3 | ||
|
|
||
| * skip |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * 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>> levelOrder(TreeNode* root) { | ||
| if (!root) return {}; | ||
| std::vector<std::vector<int>> result; | ||
| std::vector<TreeNode*> nodes = {root}; // current-level nodes. | ||
| std::vector<TreeNode*> next_nodes; // next-level nodes. | ||
|
|
||
| while (!nodes.empty()) { | ||
| std::vector<int> current_level_values; | ||
| for (auto node : nodes) { | ||
| current_level_values.push_back(node->val); | ||
| if (node->left) next_nodes.push_back(node->left); | ||
| if (node->right) next_nodes.push_back(node->right); | ||
| } | ||
| result.push_back(current_level_values); | ||
| nodes = next_nodes; | ||
|
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. std::swap(nodes, next_nodes) すると、コピーが発生しなくなり、やや軽くなると思います。 |
||
| next_nodes.clear(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * 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) {} | ||
| * }; | ||
| */ | ||
|
|
||
| #include <queue> | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<int>> levelOrder(TreeNode* root) { | ||
| if (!root) return {}; | ||
|
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. こちらのコメントをご参照ください。 |
||
| std::vector<std::vector<int>> result; | ||
| std::queue<TreeNode*> nodes; | ||
| nodes.push(root); | ||
|
|
||
| while (!nodes.empty()) { | ||
| int level_size = nodes.size(); | ||
| std::vector<int> current_level_values; | ||
| for (int i = 0; i < level_size; ++i) { | ||
|
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. nodes の先頭 level_size 個を処理するという書き方は、 BFS の書き方としてはやや分かりにくいように感じます。 step1 のようにレベルごとに異なる配列を使うか、要素に深さも一緒に入れてあげるほうが分かりやすいと思います。 |
||
| TreeNode* node = nodes.front(); | ||
| nodes.pop(); | ||
| current_level_values.push_back(node->val); | ||
| if (node->left) nodes.push(node->left); | ||
| if (node->right) nodes.push(node->right); | ||
| } | ||
| result.push_back(current_level_values); | ||
|
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. インデントがずれているようです。 |
||
| } | ||
| 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.
next_nodes はループのイテレーション間で内容を持ち越さないため、スコープを短くするため、 while の中で定義したほうが良いと思いました。