Path Sum II

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]
]
 
 
采用深度优先搜索即可
 
 /**
* 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<int> tmp;
vector<vector<int> > result;
if(root==NULL)
{
return result;
}
DFS(root,sum,tmp,result);
return result; } void DFS(TreeNode *root,int &sum, vector<int> tmp,vector<vector<int> > &result,int cur=)
{
if(root==NULL)
{
return;
} int val=root->val;
tmp.push_back(val); if(root->left==NULL&&root->right==NULL)
{
if(cur+val==sum) result.push_back(tmp);
return;
}
DFS(root->left,sum,tmp,result,cur+val);
DFS(root->right,sum,tmp,result,cur+val);
}
};

【leetcode】Path Sum II的更多相关文章

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

  2. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  3. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  4. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  5. 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径

    在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...

  6. 【LeetCode】Path Sum ---------LeetCode java 小结

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

  7. 【leetcode】Path Sum I & II(middle)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. 【Leetcode】【Medium】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 ...

  9. 【leetcode】Path Sum

    题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

随机推荐

  1. 浅谈T-SQL中的特殊联结

    引言 上一篇博客我们介绍了交叉联接,内联接,外联接3种基本的联接操作.这一篇文章我们介绍一些特殊的联接操作. 组合联接 组合联接就是联接条件涉及到联接两边的多个列的查询.当需要根据主键-外键关系来联接 ...

  2. Thinkphp 连接查询的使用

    方法一:使用table()方法 $tables = 'b_order ordert, b_busbid busbid'; $map['busbid.buscompanyid'] = 1; $map[' ...

  3. Serializer序列化/反序列化DateTime少了8小时问题解决

    1.举例子 JavascriptSerializer serializer = new JavascriptSerializer(); DateTime now = DateTime.Parse(&q ...

  4. spring的PathMatchingResourcePatternResolver-通配符的Resource查找器

    PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括: /WEB-INF/*-context.xml com/mycompany/**/ap ...

  5. IOS开发中@2x图片等适应不同分辨率手机

    开发中,例如: nanshanImage.image=[UIImage imageNamed:@'index_pic.png']; 在项目中还保存中index_pic@2x.png的图片,此图为了只适 ...

  6. Eclipse构建Maven项目

    1. 安装m2eclipse插件     要用Eclipse构建Maven项目,我们需要先安装meeclipse插件     点击eclipse菜单栏Help->Eclipse Marketpl ...

  7. Linux运维初级教程(二)账户与安全

    知识点 用户ID为UID,组ID为GID,UID=0表示超级管理员即root. 一个用户只可以加入一个基本组,但是可以同时加入多个附加组. 创建用户时,系统默认会自动创建同名的组,并设置用户加入该基本 ...

  8. [C++基础]关于对象的创建及内存分配

    测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...

  9. poj3070 (斐波那契,矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9630   Accepted: 6839 Descrip ...

  10. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...