-
Notifications
You must be signed in to change notification settings - Fork 0
103. Binary Tree Zigzag Level Order Traversal #41
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,37 @@ | ||
/* | ||
|
||
Solve Time : 11:48 | ||
|
||
Time : O(N) | ||
Space : O(N) | ||
*/ | ||
class Solution { | ||
public: | ||
vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
bool reversed_level = false; | ||
vector<vector<int>> zigzag_level_ordered_vals; | ||
vector<TreeNode*> current_level_nodes = {root}; | ||
while (!current_level_nodes.empty()) { | ||
vector<TreeNode*> next_level_nodes; | ||
vector<int> same_level_vals; | ||
for (int i = 0; i < current_level_nodes.size(); ++i) { | ||
if (!current_level_nodes[i]) { | ||
continue; | ||
} | ||
same_level_vals.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_vals.empty()) { | ||
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.
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. コメントありがとうございます。 |
||
break; | ||
} | ||
if (reversed_level) { | ||
reverse(same_level_vals.begin(), same_level_vals.end()); | ||
} | ||
reversed_level = !reversed_level; | ||
swap(current_level_nodes, next_level_nodes); | ||
zigzag_level_ordered_vals.push_back(same_level_vals); | ||
} | ||
return zigzag_level_ordered_vals; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Time: O(N) | ||
Space: O(N) | ||
|
||
nil判定を最初に置くことでwhileの条件やbreakを素直にした | ||
*/ | ||
class Solution { | ||
public: | ||
vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
if (!root) { | ||
return {}; | ||
} | ||
bool reversed_level = false; | ||
vector<vector<int>> zigzag_level_ordered_vals; | ||
vector<TreeNode*> current_level_nodes = {root}; | ||
while (!current_level_nodes.empty()) { | ||
vector<TreeNode*> next_level_nodes; | ||
vector<int> same_level_vals; | ||
for (int i = 0; i < current_level_nodes.size(); ++i) { | ||
if (!current_level_nodes[i]) { | ||
continue; | ||
} | ||
Comment on lines
+20
to
+22
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. コメントありがとうございます。 |
||
same_level_vals.push_back(current_level_nodes[i]->val); | ||
if (current_level_nodes[i]->left) { | ||
next_level_nodes.push_back(current_level_nodes[i]->left); | ||
} | ||
if (current_level_nodes[i]->right) { | ||
next_level_nodes.push_back(current_level_nodes[i]->right); | ||
} | ||
} | ||
if (reversed_level) { | ||
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. same_level_vals に関する処理と current_level_nodes, next_level_nodes に関する処理はまとめたほうが読みやすくなると思います。 same_level_vals を処理したあと、 current_level_nodes, next_level_nodes を処理してもう一度 same_level_vals を処理すると、 same_level_vals についてどのような処理を行ったのか、もう一度思い出さなければならなくなりますので。 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 (reversed_level) {
reverse(same_level_vals.begin(), same_level_vals.end());
}
reversed_level = !reversed_level;
zigzag_level_ordered_vals.push_back(same_level_vals);
swap(current_level_nodes, next_level_nodes); |
||
reverse(same_level_vals.begin(), same_level_vals.end()); | ||
} | ||
reversed_level = !reversed_level; | ||
swap(current_level_nodes, next_level_nodes); | ||
zigzag_level_ordered_vals.push_back(same_level_vals); | ||
} | ||
return zigzag_level_ordered_vals; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
if (!root) { | ||
return {}; | ||
} | ||
bool reverse_level = false; | ||
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. なるほど、ありがとうございます、先頭に |
||
vector<vector<int>> level_ordered_vals; | ||
vector<TreeNode*> current_level_nodes = {root}; | ||
while (!current_level_nodes.empty()) { | ||
vector<TreeNode*> next_level_nodes; | ||
level_ordered_vals.push_back({}); | ||
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. C詳しくなく何とも言えませんが、 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. level_ordered_vals.append([]) と思えばだいたいいいです。 |
||
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. ranged for 使ってもいいかもしれませんね。 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. コメントありがとうございます。 |
||
auto node = current_level_nodes[i]; | ||
if (node->left) { | ||
next_level_nodes.push_back(node->left); | ||
} | ||
if (node->right) { | ||
next_level_nodes.push_back(node->right); | ||
} | ||
level_ordered_vals.back().push_back(node->val); | ||
} | ||
if (reverse_level) { | ||
reverse(level_ordered_vals.back().begin(), level_ordered_vals.back().end()); | ||
} | ||
reverse_level = !reverse_level; | ||
swap(next_level_nodes, current_level_nodes); | ||
} | ||
return level_ordered_vals; | ||
} | ||
}; | ||
|
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.
currentとnextが対比で使われていて、それに対してsameというのがどっちと同じlevelなのか宣言時にわかりにくいため、自分なら
current_level_vals
とします。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.
なるほど、ありがとうございます。
current_level_nodesとの対比でわかりやすくなりますね