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. Android Studio NDK 代码 Source Insight调试 (NDK 目前开发方案 | NDK 编译 | 导入 so 库 | 项目编码转换)

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/52088039 最近在移植一个 JNI 项目, 比较纠结, A ...

  2. java集合循环删除

    java集合循环删除,java list集合操作,java循环.分享牛,分享牛原创.java集合删除方法. 2.6.1.第一种方式 list.add("1"); list.add( ...

  3. Android艺术开发探索第四章——View的工作原理(下)

    Android艺术开发探索第四章--View的工作原理(下) 我们上篇BB了这么多,这篇就多多少少要来点实战了,上篇主席叫我多点自己的理解,那我就多点真诚,少点套路了,老司机,开车吧! 我们这一篇就扯 ...

  4. (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)

    接下来,我们再讲一下图文消息: 如图: 大家可以先从开发者文档中了解一下图文消息的一些参数: 如上图,用户回复4时,ipastor返回了几条图文消息,上图中属于多图文消息,当然还有单图文消息,图文消息 ...

  5. scheme深拷贝和浅拷贝探索

    > (define a '(1 2 3)) > (define b (cons a '())) > b (( )) > (set-car! (car b) ) > b ( ...

  6. [openresty]安装nginx_lua

    这种方式是直接安装openresty ,不是通过重新编译nginx Ubuntu 安装 安装依赖包 $ sudo apt-get install libreadline-dev libncurses5 ...

  7. Spring Resource接口获取资源

    1.1.1. Resource简介 在Spring内部实现机制,针对于资源文件(配置的xml文件)有一个统一的接口Resource. 1.1.1.1. 接口定义的方法 1.exists():判断资源文 ...

  8. ThreadLocal的使用[代码片段]

    1.ThreadLocal定义,在一个类中定义: 在类A中: private static ThreadLocal<String> kcsHtmlPath = new ThreadLoca ...

  9. SSH深度历险(二) Jboss+EJB的第一个实例

    学习感悟:每次学习新的知识,都会通过第一个小的实例入手,获得成就感,经典的Hello Workd实例奠定了我们成功的大门哈,这些经典的实例虽小但是五脏俱全呢,很好的理解了,Ejb的核心. 今天主要以这 ...

  10. SVN关于忽略xcuserdata目录

    SVN关于忽略xcuserdata目录,以iPhone Qzone工程为例Xcode工程,xcuserdata目录一般位于blur.xcodeproj目录下面,eg:jonesduan-MacBook ...