zoukankan      html  css  js  c++  java
  • leetcode path sum II

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
    vector<int>v1;
    vector<vector<int>>v;
    int countsum;
        vector<vector<int> > pathSum(TreeNode *root, int sum) 
        {
            v.clear();
            v1.clear();
            if(root==NULL)return v;    
            countsum=0;
            calsum(root,sum);
            return v;        
        }
        void calsum(TreeNode *root,int &sum)
        {
            if(root)
            {
                countsum+=root->val;
                v1.push_back(root->val);
            }
            if(countsum==sum&&root->left==NULL&&root->right==NULL)
            {
                v.push_back(v1);
            }     
            if(root)
            {
              if(root->left)calsum(root->left,sum);
              if(root->right)calsum(root->right,sum);
              countsum-=root->val;
              v1.pop_back();  
            }
        }
    };
    
  • 相关阅读:
    day09
    day08
    day05
    day04
    day03
    day02
    LogCat查看Android运行日志
    ADT+SDK,Android开发环境搭建问题
    第二次冲刺周期第十天
    第二次冲刺周期第九天
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/3099337.html
Copyright © 2011-2022 走看看