());“ 意思是?,c++"/>

首页 > 解决方案 > "ret.push_back(vector) 是什么意思());“ 意思是?

问题描述

你能告诉我具体是什么ret.push_back(vector<int>());意思吗?整个代码如下:

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct Solution 
{
    vector <vector <int>> ret;
    void buildVector(TreeNode *root, int depth)
    {
        if(root==NULL) return;
        if(ret.size()==depth) ret.push_back(vector<int>());
        ret[depth].push_back(root->val);
        buildVector(root->left,depth+1);
        buildVector(root->right,depth+1);
    }
    vector<vector<int>> levelOrder(TreeNode *A) 
    {
        buildVector(A,0);
        return ret;
    }
};

标签: c++

解决方案


vector <vector<int>> ret;手段ret是向量的向量。为了容易理解这一点,vector 是一个动态数组。所以基本上,ret是一个数组数组。

因此,在这种情况下,ret.push_back(vector<int>())意味着您正在向数组数组中添加一个新数组。


推荐阅读