首页 > 学院 > 开发设计 > 正文

leetcode-113-Path Sum II

2019-11-11 02:01:57
字体:
来源:转载
供稿:网友

问题

题目:[leetcode-113I]

思路

和之前的那一道只是在接口上有区别而已。

代码

/** * 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) { std::vector<std::vector<int>> ret; std::vector<int> nodes; if(!root) return ret; dfs(root, sum, 0, nodes, ret); return ret; }PRivate: void dfs(TreeNode* root, int sum, int res, std::vector<int> nodes, std::vector<std::vector<int>>& ret){ if(root){ res += root->val; nodes.push_back(root->val); if(!root->left && !root->right){ if(res==sum) ret.push_back(nodes); } dfs( root->left, sum, res, nodes, ret ); dfs( root->right, sum, res, nodes, ret ); } }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表