C++ 17ms Solution


  • 0
    B
    class Solution {
    public:
        void dfs(TreeNode* node, int sum){
            if(node == NULL) return;
            sum = sum - node->val;
            store.push_back(node->val);
            if(node->left == NULL && node->right == NULL){//find bottom leaf
                if(sum == 0)    res.push_back(store);
            }else{
                dfs(node->left, sum);
                dfs(node->right, sum);
            }
            store.pop_back();
        }
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            dfs(root, sum);
            return res;
        }
    private:
        vector<int> store;
        vector<vector<int>> res;
    };

Log in to reply
 

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