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;

    }

};

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2021-08-12
  • 2021-12-20
  • 2021-10-29
  • 2021-06-14
猜你喜欢
  • 2021-10-28
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2021-07-06
  • 2021-08-01
相关资源
相似解决方案