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

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            vector<vector<int>> vv,vv2;
            
            if(root==NULL) return vv;
            vector<int> v;
              
            if(root->left ==NULL && root->right == NULL) {
                if(root->val==sum){
                    v.push_back(root->val);
                    vv.push_back(v);
                }
                return vv;
            }
                
            vv = pathSum(root->left,sum-root->val);
            if(!vv.empty()){
                for(int i=0;i<vv.size();i++){
                    vv[i].insert(vv[i].begin(),root->val);
                }
            }
           
            vv2 = pathSum(root->right,sum-root->val);
            if(!vv2.empty()){
                for(int i=0;i<vv2.size();i++){
                    vv2[i].insert(vv2[i].begin(),root->val);
                    vv.push_back(vv2[i]);
                }
            }
            
            return vv;
        }
    };
  • 相关阅读:
    ES6
    ES6
    ES6
    ES6
    ES6
    ES6
    ES6
    IOS 最新开发上架流程 以及发布打包注意事项
    JavaScript Arguments
    函数防抖和函数节流
  • 原文地址:https://www.cnblogs.com/julie-yang/p/4669475.html
Copyright © 2011-2022 走看看