zoukankan      html  css  js  c++  java
  • 将牛客中的代码在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;
    }
  • 相关阅读:
    微信小程序wx.chooseImage和wx.previewImage的综合使用(图片上传不限制最多张数)
    js数组与字符串之间的相互转换
    微信小程序wx.previewImage实用案例(交流QQ群:604788754)
    PHP:第一章——PHP中的魔术常量
    小程序模板嵌套以及相关遍历数据绑定
    6 大主流 Web 框架优缺点对比:15篇前端热文回看
    通俗地讲,Netty 能做什么?
    Netty
    为什么选择Netty
    linux下gsoap的初次使用 -- c风格加法实例
  • 原文地址:https://www.cnblogs.com/littleswan/p/11792733.html
Copyright © 2011-2022 走看看