【LeetCode】589. N-ary Tree Preorder Traversal

这道题没什么特别之处

但是从题解中学到了,遍历vector的新方法

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int> preorder(Node* root) {
        if (root == nullptr) return {};
        vector<int> result = {root->val};
        for (auto* child : root->children) { //遍历vector元素 (C++ 11)
            vector<int> preorder_child = preorder(child);
            result.insert(result.end(), preorder_child.begin(), preorder_child.end());
        }
        return result;
    }
};

 关于这个C++ 11 中给出的新方法,见下文:

C++ 11 for循环新方法

相关文章:

  • 2022-12-23
  • 2021-09-22
  • 2021-06-28
  • 2021-10-04
  • 2022-02-10
  • 2021-06-28
  • 2021-12-15
  • 2021-09-27
猜你喜欢
  • 2021-09-16
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-07-26
  • 2021-10-15
相关资源
相似解决方案