global vvec


  • 0
    I
    /**
     * 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> > vVec;
        vector<vector<int> > pathSum(TreeNode* root, int sum) {
            vVec.clear();
            if(nullptr==root) return vVec;
            vector<int> vec;
            binaryTreePaths(root, vec, sum);
            return vVec;
        }
        
        void binaryTreePaths(TreeNode* root, vector<int> vec, int rest){ // root cannot be nullptr
            rest -= root->val;
            vec.push_back(root->val);
            
            if(nullptr == root->left && nullptr == root->right) {
                if(rest == 0) {
                    vVec.push_back(vec);
                }
                return;
            }
            
            if(root->left) binaryTreePaths(root->left, vec, rest);
            if(root->right) binaryTreePaths(root->right, vec, rest);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.