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. python 默认全局变量

    python 内置默认全局变量print (vars()) __doc__ #py文件头部的注释 '''我是一个注释例子''' print (vars()) __file__ #当前文件路劲__pac ...

  2. 通过CSS实现的html背景色渐变

    实现代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  3. nginx禁止ip直接访问

    编辑一个noIp.conf放到虚拟目录中 server { listen default; server_name _; rewrite ^ http://www.xxxx.com/; } 其中 ww ...

  4. php各种编码集详解和以及在什么情况下进行使用

    字符是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等. 字符集是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同,常见字符集名称:ASCII字符集.GB2312字符集.B ...

  5. 《C++编程规范》

    1.使用编译器的最高警告级别,成功的构建应该是无声无息的(没有警告的). 如果确定是无害警告,且是无法修改的第三方头文件引起的,可以用自己的头文件包装起来,并有选择性的关闭警告,然后项目中使用该头文件 ...

  6. sja1000芯片can驱动程序

    应用层使用socketCan的方法:http://pan.baidu.com/s/1ntsvbb7#path=%252Floongson1%252Ftools%252Fcan 功能:对can驱动程序的 ...

  7. compareTo(String str)与compareToIgnoreCase(String str)

    一.compareTo(String str)方法 返回值:如果参数字符串等于此字符串,则返回值 0:如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值:如果此字符串按字典顺序大于字符串参 ...

  8. DateEdit和TimeEdit用法

    DateEdit 控件默认情况下,显示的只有日期,没有时间.下面介绍2中日期和时间同时显示的方法: 1.Properties.VistaDisplayMode 为true, 2.Properties. ...

  9. C#GDI+图像处理

    支持格式:BMP.GIF.JPEG.EXIF.PNG.TIFF.ICON.WMF.EMF等,几乎涵盖所有常用格式 图像类: Image类:Bitmap和Metafile的类提供功能的抽象基类. Met ...

  10. 联不上网 Unable to initialize Windows Sockets interface. General failure.

    电脑莫名联不上网 Unable to initialize Windows Sockets interface. General failure. Unable to initialize the W ...