Path Sum II
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int> >res;//记录结果 void dfs(TreeNode *root,int sum,vector<int> temp){//temp记录每一条可能的路径 if(!root) return; if(!root->left&&!root->right&&sum == root->val){//满足条件 temp.push_back(sum); res.push_back(temp); } temp.push_back(root->val); dfs(root->left,sum-root->val,temp); dfs(root->right,sum-root->val,temp); } vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<int>temp; dfs(root,sum,temp); return res; } };