leetcode-113-路径总和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>> res = {};

        if (root){

            if (root->left == NULL && root->right == NULL && root->val == sum){

                res.push_back(vector<int> ());

                res[res.size()-1].push_back(root->val);

                return res;

            }

            else {

                vector<vector<int>> temp = pathSum(root->left, sum-root->val);

                for (auto i:temp){

                    res.push_back(vector<int>());

                    res[res.size()-1].push_back(root->val);

                    for (auto j:i) res[res.size()-1].push_back(j);

                }

                temp = pathSum(root->right, sum-root->val);

                for (auto i:temp){

                    res.push_back(vector<int>());

                    res[res.size()-1].push_back(root->val);

                    for (auto j:i) res[res.size()-1].push_back(j);

                }

            }

        }

        return res;

    }

};

发布了82 篇原创文章 · 获赞 0 · 访问量 1363

猜你喜欢

转载自blog.csdn.net/ChenD17/article/details/104282402