Create 102. Binary Tree Level Order Traversal.md#29
Open
irohafternoon wants to merge 1 commit intomainfrom
Open
Create 102. Binary Tree Level Order Traversal.md#29irohafternoon wants to merge 1 commit intomainfrom
irohafternoon wants to merge 1 commit intomainfrom
Conversation
oda
reviewed
Apr 26, 2025
| return; | ||
| } | ||
| }; | ||
| ``` |
There was a problem hiding this comment.
この形はわりと素直にループになりますね。
箱を用意して {root, 0} を入れて、箱から出てきた紙を処理して {root->left, depth + 1} と {root->right, depth + 1} を入れていきます。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
以下のような形になると理解しました。
class Solution {
public:
std::vector<std::vector<int>> levelOrder(TreeNode* root) {
std::vector<std::vector<int>> level_ordered_values;
std::queue<std::pair<TreeNode*, int>> node_and_depth;
node_and_depth.emplace(root, 0);
while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();
if (!node) {
continue;
}
if (depth == level_ordered_values.size()) {
level_ordered_values.emplace_back();
}
level_ordered_values[depth].push_back(node->val);
node_and_depth.emplace(node->left, depth + 1);
node_and_depth.emplace(node->right, depth + 1);
}
return level_ordered_values;
}
};
Fuminiton
reviewed
Apr 27, 2025
| level_ordered_values[depth].push_back(root->val); | ||
| ContructLeverOrderValues(root->left, depth + 1, level_ordered_values); | ||
| ContructLeverOrderValues(root->right, depth + 1, level_ordered_values); | ||
| return; |
There was a problem hiding this comment.
良いと思いました。
148~150行目は順番を変えても成り立ち、それぞれどのような順で木を走査することになるかは実験してみてもいいかもしれません。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます!
本問の場合は走査すべき順番が固定(各ステップで左→右でないといけない)されているので入れ替えられないと理解しています
以下のように入れ替えたコードでは誤りになりました
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> levelOrder(TreeNode* root) {
std::vector<std::vector<int>> level_ordered_values;
ContructLeverOrderValues(root, 0, level_ordered_values);
return level_ordered_values;
}
private:
void ContructLeverOrderValues(TreeNode* root, int depth,
std::vector<std::vector<int>>& level_ordered_values) {
if (!root) {
return;
}
if (depth == level_ordered_values.size()) {
level_ordered_values.emplace_back();
}
level_ordered_values[depth].push_back(root->val);
ContructLeverOrderValues(root->right, depth + 1, level_ordered_values);
ContructLeverOrderValues(root->left, depth + 1, level_ordered_values);
return;
}
};
fuga-98
reviewed
Apr 27, 2025
| return; | ||
| } | ||
| }; | ||
| ``` |
There was a problem hiding this comment.
これなら副作用のない関数で実装できるのではと思いました。
olsen-blue/Arai60#29 (comment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This Problem
Binary Tree Level Order Traversal
Next Problem
Binary Tree Zigzag Level Order Traversal