-
Notifications
You must be signed in to change notification settings - Fork 0
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
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,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; | ||
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; | ||
} | ||
}; |
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; | ||
} | ||
}; |
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; | ||
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. 自分はTreeNodeのメンバ変数valに合わせて 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. ありがとうございます、確かに名前付けの一貫性は大事ですね。 |
||
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) { | ||
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. ここを 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. ありがとうございます、確かにこちらでも良さそうですね |
||
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); | ||
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. 単に 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. swapの方が速いのと、読みやすさですね |
||
} | ||
return level_ordered_nums; | ||
} | ||
}; |
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; | ||
} | ||
}; |
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.
自分ならこのタイミングで
max(left.size(), right.size())
要素で初期化し、 current_level_vals を経由せずに直接 insert すると思います。current_level_vals
の push_back() にかかるコピーを省けますので。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_backでコピー発生は考慮できていなかったです・・・。