标签:
利用递归处理
/** * 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> >ans; vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<int>v; Sum(root,sum,v); return ans; } void Sum(TreeNode *root, int sum, vector<int>res){ if(root == NULL) return ; res.push_back(root->val); if(root->val == sum && root->left == NULL && root->right == NULL) ans.push_back(res); Sum(root->left,sum-root->val,res); Sum(root->right,sum-root->val,res); } };
标签:
原文地址:http://blog.csdn.net/sina012345/article/details/43103105