Skip to content

Create 102. Binary Tree Level Order Traversal.md#29

Open
irohafternoon wants to merge 1 commit intomainfrom
102.-Binary-Tree-Level-Order-Traversal
Open

Create 102. Binary Tree Level Order Traversal.md#29
irohafternoon wants to merge 1 commit intomainfrom
102.-Binary-Tree-Level-Order-Traversal

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

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.

この形はわりと素直にループになりますね。

箱を用意して {root, 0} を入れて、箱から出てきた紙を処理して {root->left, depth + 1} と {root->right, depth + 1} を入れていきます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
以下のような形になると理解しました。

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;
    }
};

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

Choose a reason for hiding this comment

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

良いと思いました。
148~150行目は順番を変えても成り立ち、それぞれどのような順で木を走査することになるかは実験してみてもいいかもしれません。

https://en.wikipedia.org/wiki/Tree_traversal

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!

本問の場合は走査すべき順番が固定(各ステップで左→右でないといけない)されているので入れ替えられないと理解しています
以下のように入れ替えたコードでは誤りになりました

#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;
    }
};

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

Choose a reason for hiding this comment

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

これなら副作用のない関数で実装できるのではと思いました。
olsen-blue/Arai60#29 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants