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,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

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

Tree Depth-first Search

 

   这题其实就是深度搜索,代码:
#include <vector>
#include <iostream>
using namespace std;
/**
* 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> > ret;
vector<int> nowPath;
vector<vector<int> > pathSum(TreeNode *root, int sum) {
ret.clear();
nowPath.clear();
if(root==NULL) return ret;
help_f(root,sum);
return ret;
} void help_f(TreeNode *root,int sum)
{
if(root==NULL) return ;
nowPath.push_back(root->val);
help_f(root->left,sum - root->val);
help_f(root->right,sum - root->val);
if(root->left==NULL&&root->right==NULL&&sum==root->val)
ret.push_back(nowPath);
nowPath.pop_back();
}
}; int main()
{
return ;
}

[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. crontab -e 和/etc/crontab的区别

    /etc/crontab文件和crontab -e命令区别/etc/crontab文件和crontab -e命令区别 1.格式不同 前者 # For details see man 4 crontab ...

  2. nuxt.js express模板项目IIS部署

    继续上一篇的nuxt/express项目部署,还是windows上要把nuxt的服务端渲染项目跑起来,这次的目的是用已经有的域名windows服务器上一个虚拟目录反向代理部署在其他端口nuxt项目. ...

  3. Gym - 101981D Country Meow(模拟退火)

    题意 三维空间有\(n\)个点,找到另外一个点,离所有点的最大距离最小.求这个距离. 题解 \(1\).最小球覆盖,要找的点为球心. \(2\).模拟退火. 还是补一下模拟退火的介绍吧. 模拟退火有一 ...

  4. 7、python中的字典

    字典是python内置的一种无序.可变的数据结构. 字典也叫哈希表.什么是哈希表?哈希表就是会对表中的键(key)执行哈希计算,并根据计算结果在内存中分配一个区域来储存该键所对应的值(value).这 ...

  5. opencv使用日记之一:平台搭建Mat类以及图像的读取修改

    平台搭建就摸了一整天时间,真的是...不说了,最后我选择的是 opencv3.0(2015/06/04)  + win7 + vs2012   注意opencv的版本不同导入的库文件是不一样的,所以请 ...

  6. 命令执行sql

    从外网把数据库用导出脚本的方式导出来了,280M的样子,导是导出来了,但是在本机执行sql脚本的时候,直接就是out of memory,之前执行60M的脚本没出过这问题,直接双击打开.sql脚本文件 ...

  7. Go语言之并发编程(四)

    同步 Go 程序可以使用通道进行多个 goroutine 间的数据交换,但这仅仅是数据同步中的一种方法.通道内部的实现依然使用了各种锁,因此优雅代码的代价是性能.在某些轻量级的场合,原子访问(atom ...

  8. tomcat缓存

    问题描述: 一个用到struts2框架的web项目,由于在struts.xml中少配置了一个action,导致项目运行时报异常.将原本好的代码复旧,重启tomcat服务,第一次加载程序没问题,再刷新时 ...

  9. wim

    wim 编辑 WIM是英文Microsoft Windows Imaging Format(WIM)的简称,它是Windows基于文件的映像格式.WIM 映像格式并非现在相当常见的基于扇区的映像格式, ...

  10. C 语言 习题 1-14

    练习 1-14 编写一个程序,打印输入中各个字符出现频度的直方图. #include <stdio.h> /* count digits, white space, others */ i ...