Path Sum

  题目链接

  题目要求:

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

  For example:
  Given the below binary tree and sum = 22,

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

  return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

  这道题采用深度优先搜索的方法就可以了,具体程序(12ms)如下:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
return hasPathSumSub(root, , sum);
} bool hasPathSumSub(TreeNode *tree, int subSum, int sum)
{
if(!tree)
return false; subSum += tree->val;
if(!tree->left && !tree->right && subSum == sum)
return true; return hasPathSumSub(tree->left, subSum, sum) || hasPathSumSub(tree->right, subSum, sum);
}
};

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]
]

  这题与上题类似。具体程序(24ms)如下:

 /**
* Definition for a binary tree node.
* 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<vector<int>> rVec;
if(!root)
return rVec; vector<int> vec;
hasPathSumSub(root, vec, rVec, , sum);
return rVec;
} void hasPathSumSub(TreeNode *tree, vector<int> vec, vector<vector<int>>& rVec, int subSum, int sum)
{
if(!tree)
return; vec.push_back(tree->val);
subSum += tree->val;
if(!tree->left && !tree->right && subSum == sum)
rVec.push_back(vec); hasPathSumSub(tree->left, vec, rVec, subSum, sum);
hasPathSumSub(tree->right, vec, rVec, subSum, sum);
}
};

LeetCode之“树”:Path Sum && Path Sum II的更多相关文章

  1. Leetcode题 112 和 113. Path Sum I and II

    112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  3. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

  4. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  5. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  7. 【Leetcode】【Easy】Path Sum

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

  8. 【leetcode刷题笔记】Path Sum

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

  9. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. python 列表解析与map和filter函数

    不知哪儿看到一个说法,大概是当map的函数参数可以直接引用一个已有的函数变量时(比如内建函数int,str之类的),用map更优美些,否则还是用列表解析更直观和快速. 我同意此说法. 昨天在写一个函数 ...

  2. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  3. 《高性能MySQL》读书笔记(上)

    <High Performance MySQL>真是本经典好书,从应用层到数据库到硬件平台,各种调优技巧.常见问题全都有所提及.数据库的各种概念技巧平时都有接触,像索引.分区.Shardi ...

  4. 【移动开发】EditText输入字数限制总结(包括中文输入内存溢出的解决方法)

    限定EditText输入个数的解决方案很多,但是一般主要考虑两点,也就是处理两件事:(1)不同语言字符(英文.中文等)处理方式(2)输入字符达到数目后,是否仍然允许用户输入 第一点,涉及的东东其实蛮多 ...

  5. 如何查看App provision profile文件中的钥匙链访问组名称

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们因为某些原因希望安全的在多个App中共享一些信息,我们可以 ...

  6. 即时通讯软件openfire+spark+smack

    所以我基本上分为三篇文章来介绍此类软件的开发: 第一篇是关于XMPP 协议是啥,IM 是啥以及一个比较有名的开源实现,该开源实现包括三个部分(Spark.Smack和Openfire): 第二篇讲如何 ...

  7. Android简易实战教程--第十三话《短信备份和还原~三》

    之前写过短信备份的小案例,哪里仅仅是虚拟了几条短信信息.本篇封装一个业务类,且直接通过内容提供者,访问本系统的短信信息,再提供对外接口.如果想要短信备份和短信还原,直接复制这段代码即可.对于您调用这个 ...

  8. Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、

    LineNumberReader 跟综行号的缓冲字符输入流,些类定义了setLineNumber(int)和getLineNumber(int),它们可分别用于设置和获取当前行号 import jav ...

  9. UNIX网络编程——产生RST

    产生RST的3个条件:1. 建立连接的SYN到达某端口,但是该端口上没有正在监听的服务.   如:IP为192.168.1.33的主机上并没有开启WEB服务(端口号为0x50),这时我们通过IE去访问 ...

  10. 免安装版本tomcat 指定的服务并未以已安装的服务存在,Unable to open the service

    今天在自己的电脑上安装了Tomcat6.0.14,是在Tomcat主页上直接下载的免安装版.但是把文件解压的之后,双击Tomcat6w.exe时,去出现了"指定的服务并未以已安装的服务存在, ...