码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Path Sum II

时间:2015-07-12 00:14:13      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

Well, a typical backtracking problem. The code is as follows. You may walk through it using the example in the problem statement to see how it works.

 1 class Solution {
 2 public:
 3     vector<vector<int>> pathSum(TreeNode* root, int sum) {
 4         vector<vector<int> > paths;
 5         vector<int> path;
 6         findPaths(root, sum, path, paths);
 7         return paths;  
 8     }
 9 private:
10     void findPaths(TreeNode* node, int sum, vector<int>& path, vector<vector<int> >& paths) {
11         if (!node) return;
12         path.push_back(node -> val);
13         if (!(node -> left) && !(node -> right) && sum == node -> val)
14             paths.push_back(path);
15         findPaths(node -> left, sum - node -> val, path, paths);
16         findPaths(node -> right, sum - node -> val, path, paths);
17         path.pop_back();
18     }
19 };

 

[LeetCode] Path Sum II

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4639562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!