Skip to content
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

102. Binary Tree Level Order Traversal #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
36 changes: 36 additions & 0 deletions 102/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Solve Time: 21:38

Time : O(N^2)
Space : O(N^2)
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if (!root) {
return {};
}
auto left = levelOrder(root->left);
auto right = levelOrder(root->right);
auto merged_level_vals = merge_two_trees(left, right);
vector<vector<int>> level_order = {{root->val}};
level_order.insert(level_order.end(), merged_level_vals.begin(), merged_level_vals.end());
return level_order;
}

private:
vector<vector<int>> merge_two_trees(vector<vector<int>>& left, vector<vector<int>>& right) {
vector<vector<int>> merged_levels;
Copy link

Choose a reason for hiding this comment

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

自分ならこのタイミングで max(left.size(), right.size()) 要素で初期化し、 current_level_vals を経由せずに直接 insert すると思います。 current_level_vals の push_back() にかかるコピーを省けますので。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、コメントありがとうございます。
push_backでコピー発生は考慮できていなかったです・・・。

for (int i = 0; i < max(left.size(), right.size()); ++i) {
vector<int> current_level_vals;
if (i < left.size()) {
current_level_vals.insert(current_level_vals.end(), left[i].begin(), left[i].end());
}
if (i < right.size()) {
current_level_vals.insert(current_level_vals.end(), right[i].begin(), right[i].end());
}
merged_levels.push_back(current_level_vals);
}
return merged_levels;
}
};
37 changes: 37 additions & 0 deletions 102/step2_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Time : O(N)
Space : O(N)

再帰を用いずに書いてみる。
currentはnextとの対比として自然なので使用した。
XXX_level_nodesはちょっと変数名がくどすぎる気もしたが、今見ているlevelというのを強調できるのでcurrent_nodesなどよりも適切に感じる。

*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> level_order;
queue<TreeNode*> current_level_nodes;
queue<TreeNode*> next_level_nodes;
current_level_nodes.push(root);
while (true) {
vector<int> same_level_vals;
while (!current_level_nodes.empty()) {
auto node = current_level_nodes.front();
current_level_nodes.pop();
if (!node) {
continue;
}
same_level_vals.push_back(node->val);
next_level_nodes.push(node->left);
next_level_nodes.push(node->right);
}
swap(current_level_nodes, next_level_nodes);
if (same_level_vals.empty()) {
break;
}
level_order.push_back(same_level_vals);
}
return level_order;
}
};
25 changes: 25 additions & 0 deletions 102/step2_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> level_ordered_nums;
Copy link

Choose a reason for hiding this comment

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

自分はTreeNodeのメンバ変数valに合わせてlevel_ordered_valsにするかなと思いました

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、確かに名前付けの一貫性は大事ですね。

vector<TreeNode*> current_level_nodes = {root};
while (!current_level_nodes.empty()) {
vector<TreeNode*> next_level_nodes;
vector<int> same_level_nums;
for (int i = 0; i < current_level_nodes.size(); ++i) {
Copy link

Choose a reason for hiding this comment

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

ここをfor (auto node : current_level_nodes)にするとこのブロックがすっきりすると思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、確かにこちらでも良さそうですね

if (!current_level_nodes[i]) {
continue;
}
same_level_nums.push_back(current_level_nodes[i]->val);
next_level_nodes.push_back(current_level_nodes[i]->left);
next_level_nodes.push_back(current_level_nodes[i]->right);
}
if (same_level_nums.empty()) {
break;
}
level_ordered_nums.push_back(same_level_nums);
swap(current_level_nodes, next_level_nodes);
Copy link

Choose a reason for hiding this comment

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

単にcurrent_level_nodes = next_level_nodesでもいいと思うのですが、swapを選んだ理由があったりしますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

swapの方が速いのと、読みやすさですね

}
return level_ordered_nums;
}
};
22 changes: 22 additions & 0 deletions 102/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if (!root) {
return {};
}
vector<vector<int>> level_order = {{root->val}};
auto left_level_order = levelOrder(root->left);
auto right_level_order = levelOrder(root->right);
for (int i = 0; i < max(left_level_order.size(), right_level_order.size()); ++i) {
vector<int> same_level_values;
if (i < left_level_order.size()) {
same_level_values.insert(same_level_values.end(), left_level_order[i].begin(), left_level_order[i].end());
}
if (i < right_level_order.size()) {
same_level_values.insert(same_level_values.end(), right_level_order[i].begin(), right_level_order[i].end());
}
level_order.push_back(same_level_values);
}
return level_order;
}
};