functionclass[LeetCode]Path Sum II
在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
//record all the path, though dfs is not time efficient bu also work at most time
public:
void DFS(TreeNode* root, int curSum, vector<int> curPath, vector<vector<int>>& ansPath, int sum)
{
if(!root)
return;
curSum += root->val;
curPath.push_back(root->val);
if(!root->left && !root->right)
{
if(curSum == sum)
ansPath.push_back(curPath);
return;
}
DFS(root->left, curSum, curPath, ansPath, sum);
DFS(root->right, curSum, curPath, ansPath, sum);
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ansPath;
vector<int> curPath;
DFS(root, 0, curPath, ansPath, sum);
return ansPath;
}
};
文章结束给大家分享下程序员的一些笑话语录: 手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn
---------------------------------
原创文章 By
function和class
---------------------------------

浙公网安备 33010602011771号