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 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. ]
Hide Tags

Tree Depth-first Search

 

   这题其实就是深度搜索,代码:
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. /**
  5. * Definition for binary tree
  6. */
  7. struct TreeNode {
  8. int val;
  9. TreeNode *left;
  10. TreeNode *right;
  11. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  12. };
  13.  
  14. class Solution {
  15. public:
  16. vector<vector<int> > ret;
  17. vector<int> nowPath;
  18. vector<vector<int> > pathSum(TreeNode *root, int sum) {
  19. ret.clear();
  20. nowPath.clear();
  21. if(root==NULL) return ret;
  22. help_f(root,sum);
  23. return ret;
  24. }
  25.  
  26. void help_f(TreeNode *root,int sum)
  27. {
  28. if(root==NULL) return ;
  29. nowPath.push_back(root->val);
  30. help_f(root->left,sum - root->val);
  31. help_f(root->right,sum - root->val);
  32. if(root->left==NULL&&root->right==NULL&&sum==root->val)
  33. ret.push_back(nowPath);
  34. nowPath.pop_back();
  35. }
  36. };
  37.  
  38. int main()
  39. {
  40. return ;
  41. }

[LeetCode] Path Sum II 深度搜索的更多相关文章

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

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

  3. [LeetCode] Path Sum II 二叉树路径之和之二

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

  4. [Leetcode] Path Sum II路径和

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

  5. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  6. leetcode: Path Sum II 迭代法

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

  7. LeetCode——Path Sum II

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

  8. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  9. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

随机推荐

  1. windows下简单使用pip

    1. 在python官网上下载python时会自带pip,并且在安装Python时若未取消会默认一并安装 2. 找出pip.exe所在位置, 3. 右击此电脑,点击属性 4. 高级系统设置 5. 点击 ...

  2. 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程

    点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...

  3. Roads in the North POJ - 2631

    Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...

  4. 菜鸟学Linux - Linux文件属性

    在Linux中,文件的属性是一个很重要的概念,用户或者用户组对一个文件所拥有的权限,都可以从文件的属性得知. 我们可以通过ls -al命令,列出某个文件夹下面的所有文件(包括以.开头的隐藏文件).下面 ...

  5. bzoj2733: [HNOI2012]永无乡(splay)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3778  Solved: 2020 Description 永 ...

  6. ECharts的x轴和y轴均使用数值类型

    今天有个需求,就是需要ECharts的x轴和y轴都要使用数值类型,即xAxis.type和yAxis.type均为value,然后我按照我以为的方式修改了下,发现图崩了 发现问题: 然后我打开了ECh ...

  7. vue/vux编译时出现 unexpected token <11:0-485>

    最近开发Vux项目,一直使用VS Code开发工具,可以格式化里面的<script>代码的: 但是今天突然无法格式化代码,而且编译报错.主要提示类似:unexpected token  & ...

  8. android基础知识杂记

    Activity中获取视图组件对象:public View findViewById(@IdRes int id) 该方法以组件的资源ID为参数,返回一个视图对象View,需要强转成具体的视图类对象. ...

  9. Algorithms(fourth edition)——无向图

    1.设计图基本操作API 2.用什么数据结构来表示图并实现API 要求:(1)要预留足够空间 (2)实例方法实现要快 三个选择: 邻接矩阵:布尔矩阵,不满足条件一,而且无法表示平行边 边的数组:不满足 ...

  10. JS的跨域理解

    前言 周一的学院点开题被批的很惨,换了个校长,各种被抓严,班上已经有两个同学打算休学了.哎,这周的聚会可能是大家集聚的最后一次吧.熬着吧,还是学习我的前端,不管老板学校咋逼了,找个好工作才是王道.今天 ...