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++

  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. vector<vector<int> > pathSum(TreeNode *root, int sum) {
  13. vector<vector<int>> dp;
  14. vector<int> path;
  15. getPath(root,sum,dp,path);
  16. return dp;
  17. }
  18. void getPath(TreeNode *root, int sum,vector<vector<int>>& dp,vector<int> path){
  19. if(NULL == root) return;
  20. path.push_back(root->val);
  21. if(NULL == root->left && NULL == root->right && root->val == sum )
  22. dp.push_back(path);
  23. getPath(root->left,sum - root->val,dp,path);
  24. getPath(root->right,sum - root->val,dp,path);
  25. }
  26. };

path-sum-ii leetcode C++的更多相关文章

  1. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  2. Path Sum II leetcode java

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

  3. Path Sum II——LeetCode

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

  4. 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 ...

  5. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  6. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  7. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. [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 ...

  10. 【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 ...

随机推荐

  1. sunny 内网穿透使用。

    启动方法:

  2. 关于open falcon 与nightingale 的一些调研

    针对 open-falcon 与 nightingale 的调研 一.open-falcon 1.1 组件介绍 1.1.1 agent > agent用于采集机器负载监控指标,比如cpu.idl ...

  3. 2020牛客NOIP赛前集训营-提高组(第三场)C-牛半仙的妹子Tree【虚树,最短路】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/7609/C 题目大意 给出\(n\)个点的一棵树,\(m\)个时刻各有一个操作 标记一个点,每个点被标记后的每 ...

  4. 05-IdentityServer4

    前面我们认识了jwt的token颁发模式,其中的应用场景和部分缺陷已经很是了解了.有些场合并不适合jwt,特别是针对第三方进行使用时,比如我们使用qq或者微信登陆博客园或其他第三方应用时. Ids4的 ...

  5. IceCream in Python

    IceCream in Python 你还在使用print 在Python 中 debug 吗?赶快使用Icecream吧. 提到 Icecream,你是不是会想到ta? 动机 如果你使用print去 ...

  6. Matlab 速记

    链接:https://zhuanlan.zhihu.com/p/370259237 % 1.进度提醒 f = waitbar(0,'1','Name','进度'); set(f,'color','w' ...

  7. Mysql集群搭建(多实例、主从)

    1 MySQL多实例 一 .MySQL多实例介绍 1.什么是MySQL多实例 MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307,3308),运行多个MySQL服务进程,通 ...

  8. 简单Tab切换

    延迟Tab切换,使用css中的flex布局,原生js实现.(京东首页菜单也有此延迟功能哦!) 每天进步一丢丢~~ 1.延迟Tab切换 <!DOCTYPE html> <html la ...

  9. 透过 Chrome 深入理解浏览器导航过程

    网络的导航,是从输入 url 到最终获取到文件的过程.其中牵扯到浏览器架构.操作系统.网络等一系列知识.本文将从各个角度详细论述这一过程,涉及广度与深度.如果您是已经有一定基础的同学,那么本文可以快速 ...

  10. Web前端安全之安全编码原则

    随着Web和移动应用等的快速发展,越来越多的Web安全问题逐渐显示出来.一个网站或一个移动应用,如果没有做好相关的安全防范工作,不仅会造成用户信息.服务器或数据库信息的泄露,更可能会造成用户财产的损失 ...