上一题类似,这里是要记录每条路径并返回结果。

Given the below binary tree and sum = 22,

  1. 5
  2. / \
  3. 4 8
  4. / / \
  5. 11 13 4
  6. / \ / \
  7. 7 2 5 1

return

  1. [
  2. [5,4,11,2],
  3. [5,8,4,5]
  4. ]
  5.  
  6. 我们用一个子函数来递归记录,知道叶子节点才判断是否有符合值,有的话就记录。需要注意的是递归右子树之前要把左子树的相应操作去除(见注释)。
  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12.  
  13. void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> tmp, int subsum, int sum)
  14. {
  15. if (!root) return ;
  16. if (!root -> left && !root -> right && subsum + root -> val == sum)
  17. {
  18. tmp.push_back(root -> val);
  19. ans.push_back(tmp);
  20. }
  21.  
  22. if (root -> left)
  23. {
  24. tmp.push_back(root -> val);
  25. subsum += root -> val;
  26. pathSum(root -> left, ans, tmp, subsum, sum);
  27. tmp.pop_back(); //因为判断右子树的时候不需要左子树的和
  28. subsum -= root -> val;
  29. }
  30. if (root -> right)
  31. {
  32. tmp.push_back(root -> val);
  33. subsum += root -> val;
  34. pathSum(root -> right, ans, tmp, subsum, sum);
  35. }
  36. }
  37. vector<vector<int> > pathSum(TreeNode *root, int sum)
  38. {
  39. vector<vector<int> > ans;
  40. vector<int> tmp;
  41.  
  42. pathSum(root, ans, tmp, , sum);
  43. return ans;
  44. }
  45. };

其实效率好一些的是对tmp传入引用,例如vector<int> &tmp,那么此时每次记录结果或者左右递归之后都要有一个pop值,来保证tmp符合当前的要求:详见

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12.  
  13. void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> &tmp, int subsum, int sum)
  14. {
  15. if (!root) return ;
  16. if (!root -> left && !root -> right && subsum + root -> val == sum)
  17. {
  18. tmp.push_back(root -> val);
  19. ans.push_back(tmp);
  20. tmp.pop_back(); // 保持tmp
  21. }
  22.  
  23. if (root -> left)
  24. {
  25. tmp.push_back(root -> val);
  26. subsum += root -> val;
  27. pathSum(root -> left, ans, tmp, subsum, sum);
  28. tmp.pop_back(); // 因为判断右子树的时候不需要左子树的和
  29. subsum -= root -> val; // 同上理
  30. }
  31. if (root -> right)
  32. {
  33. tmp.push_back(root -> val);
  34. subsum += root -> val;
  35. pathSum(root -> right, ans, tmp, subsum, sum);
  36. tmp.pop_back(); // 保持tmp
  37. }
  38. }
  39. vector<vector<int> > pathSum(TreeNode *root, int sum)
  40. {
  41. vector<vector<int> > ans;
  42. vector<int> tmp;
  43.  
  44. pathSum(root, ans, tmp, , sum);
  45. return ans;
  46. }
  47. };

leetco Path Sum II的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  3. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  4. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  5. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  6. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  7. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  8. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

随机推荐

  1. ASN.1 Encode an Object Identifier (OID) with OpenSSL

    OID(Object Identifier) denotes an object. Examples: ------------------------------------------------ ...

  2. NSIS:迅雷5.8.6.600自由定制版脚本及下载

    原文 NSIS:迅雷5.8.6.600自由定制版脚本及下载 虽然现在迅雷已升级到7.2版本,但作为一款下载工具,我们可能只是需要它的下载功能,所以,体积小资源占用也小的迅雷5便成了我们更好的选择.今天 ...

  3. svn常见错误汇总

    comment中的换行.把换行去掉就可以了

  4. 【C/C++学院】(24)Oracle数据库编程--管理oracle

    一.启动和停止oracle 停止和启动oracle须要切换到oracle用户才干够,其它用户都没有权限启动和停止oracle(包含root也没有权限). 1.执行sqlplus但不登录到oracle: ...

  5. hdu4288 Coder 2012成都网络赛 A题

    题意:往集合里面添加删除数,集合中的数是按从小到大排列的,询问下标模5等于3的数的和. 记得当时这题不会做, 现在想简单多了,只要维护五个值和左右子树的size大小就行了. #define maxn ...

  6. ABP领域层——领域事件(Domain events)

    ABP领域层——领域事件(Domain events) 基于DDD的现代ASP.NET开发框架--ABP系列之14.ABP领域层——领域事件(Domain events) ABP是“ASP.NET B ...

  7. hdu 1542 Atlantis 段树区,并寻求,,,尼玛真坑人数据,不要打开一小阵!

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. LeetCodeOJ. Maximum Depth of Binary Tree

    见问题: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ 主题概述 Given a binary tree, find i ...

  9. 数学思想方法-sasMEMO(17)

    SAS日期及时间格式 data  _null_;input mydate YYMMDD10.;put mydate YYMMDDB10.;put mydate YYMMDDC10.;put mydat ...

  10. myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully

    mysql 8小时问题的解决方法 转发: 别看是英文 ,写的很好 ,才转 Use Hibernate + MYSQL database development, link timeout proble ...