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

For example: Given the below binary tree andsum = 22, 5 / 

4 8 / / 

11 13 4 / \ / 

7 2 5 1 return

[ [5,4,11,2], [5,8,4,5] ]

C++

/**
* 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> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> dp;
vector<int> path;
getPath(root,sum,dp,path);
return dp;
}
void getPath(TreeNode *root, int sum,vector<vector<int>>& dp,vector<int> path){
if(NULL == root) return;
path.push_back(root->val);
if(NULL == root->left && NULL == root->right && root->val == sum )
dp.push_back(path);
getPath(root->left,sum - root->val,dp,path);
getPath(root->right,sum - root->val,dp,path);
}
};

最新文章

  1. Boostrap入门(一)
  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(39)-在线人数统计探讨
  3. Code Complete 笔记—— 第二章 用隐喻来更充分理解软件开发
  4. 项目集成ReactiveCocoa遇到的坑及解决办法
  5. webapp中的meta
  6. zpf 路由功能
  7. winform小程序---猜拳小游戏
  8. OD调试篇9
  9. Git Shell 安装版本
  10. [HDOJ5943]Kingdom of Obsession(最大匹配,思路)
  11. Android Activity的切换动画
  12. jquery文本折叠
  13. .Echo 命令中经常提到回显,是什么意思?
  14. VS SQL 出现%CommonDir%dte80a.olb 该解决方案
  15. SQL的自增列重置的方法
  16. ●CodeChef Sereja and Game
  17. 再探go modules:使用与细节
  18. 测序数据质控-FastQC
  19. 《python语言程序设计》_第二章笔记
  20. java常见错误总结

热门文章

  1. 简单学习PHP中的层次性能分析器
  2. dedecms织梦调用指定文章id
  3. Java面向对象系列(7)- 什么是继承
  4. Linux系列(42) - 防火墙相关命令
  5. Linux系列(33)- rpm命令管理之RPM包校验提取(5)
  6. Python+Pygame开发太空大战/飞机大战完整游戏项目(附源代码)
  7. CI框架 core
  8. Css3 3D 旋转动画效果
  9. 华为云计算IE面试笔记-华为云计算解决方案业务迁移支持哪些迁移?有哪些特点?请描述基本的业务交付流程、业务迁移流程和原则。
  10. 关于go mod 的使用和goland 配置 go mod