将牛客中的代码在codeblocks中进行实现

一直都是在牛客上写代码,今天想要用软件将牛客上面的代码进行实现,但是因为牛客中的代码在使用向量的时候变量的定义,像向量和二叉树,这些我们都不用写,直接就可以拿着用,所以我今天就将牛客上面的一道题进行全部的实现。将里面的一些细节进行变现。下面给出关于牛客中的一道有关向量和二叉树的题进行完整的实现:

(全代码)输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

#include <vector>
//#include<stdlib.h>
#include<iostream>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
};
class Solution {
public:
    vector<vector<int> > buffer;
    vector<int> tmp;
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root==NULL)
            return buffer;
        tmp.push_back(root->val);
        if((expectNumber-root->val)==0 && root->left==NULL && root->right==NULL)
            {
            buffer.push_back(tmp);
        }
        FindPath(root->left,expectNumber-root->val);
        FindPath(root->right,expectNumber-root->val);
        if(tmp.size()!=0)
            tmp.pop_back();
        return buffer;
    }

};
int main()
{
        TreeNode *test =new TreeNode();
        test->val=4;
        TreeNode *p,*p1;
        p=test->left=new  TreeNode();
        p->val=8;
        p1=test->right=new TreeNode();
        p1->val=5;
        p=p->left=new TreeNode();
        p->val=3;
        p1->left=new TreeNode();
        p1->left->val=7;
        p1=p1->right=new TreeNode();
        p1->val=9;
        /*p=test->right=new TreeNode();
        p->val=1;
        p1=p->left=new TreeNode();
        p=p->right=new TreeNode();
        p1->val=4;
        p->val=6;*/
        cout<<"现在是对了吗?"<<endl;
        cout<<"我真的是非常的开心了"<<endl;
        Solution *test2=new Solution();
        vector<vector<int> > buffer2=test2->FindPath(test,16);
         for (int i = 0; i < buffer2.size(); i++)
        {
        for(int j = 0; j < buffer2[0].size(); j++)
            cout << buffer2[i][j] << " ";
        cout << endl;
        }
        return 0;
}
原文地址:https://www.cnblogs.com/littleswan/p/11792733.html