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. 第五篇:selenium调用IE问题(Protected Mode settings are not the same for all zones)

    代码信息: driver = webdriver.Ie()driver.get('http://www.baidu.com') 问题描述: raise exception_class(message, ...

  2. Cluster - 基本概念

    1.1 什么是集群 简单的说,集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从来不会意识到集群系统 ...

  3. java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServiceImpl

    java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServi ...

  4. 数据存储之json文件处理和csv文件处理

    什么是json: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集,采用 ...

  5. mysql密码正确却提示错误, 不输入密码反而能登录

    今天部署阿里云服务器, 发现之前可以连接的mysql服务器突然连接不上了, 密码我确认是正确的,但登录时就是显示密码错误, 很崩溃, 差点气得我就想重装mysql了. 好在经过几番苦寻找到了以下能解决 ...

  6. 2016-2017 ACM-ICPC CHINA-Final

    A Gym 101194A Number Theory Problem 7 的二进制是111,2k-1 的二进制是 k 个 1.所以 k 能被 3 整除时 2k-1 才能被 7 整除. #includ ...

  7. 动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击

    题目: 1010: 魔兽争霸之最后的反击                                                                         Time Li ...

  8. [原]sencha touch之表单(login demo)

    现在来说说sencha touch中的表单,举个简单的login的例子,相关的说明我都放在了注释中,看下面代码 Ext.application({ id:'itKingApp', launch:fun ...

  9. Java多线程-yield(),sleep()以及wait()的区别

    从操作系统的角度讲,os会维护一个ready queue(就绪的线程队列).并且在某一时刻cpu只为ready queue中位于队列头部的线程服务.但是当前正在被服务的线程可能觉得cpu的服务质量不够 ...

  10. OpenResty安装与hello world

    安装环境:CentOS 7.0 1. 安装编译工具.依赖库 yum -y install readline-devel pcre-devel openssl-devel gcc 2. 下载openre ...