Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 102_binary_tree_level_order_traversal/memo.md
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
34 changes: 34 additions & 0 deletions 102_binary_tree_level_order_traversal/step1.cpp
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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

next_nodes はループのイテレーション間で内容を持ち越さないため、スコープを短くするため、 while の中で定義したほうが良いと思いました。


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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::swap(nodes, next_nodes) すると、コピーが発生しなくなり、やや軽くなると思います。

next_nodes.clear();
}

return result;
}
};
37 changes: 37 additions & 0 deletions 102_binary_tree_level_order_traversal/step2.cpp
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 {};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

インデントがずれているようです。

}
return result;
}
};
Empty file.