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. java记事本开发

    今天我想要分享的是一个用java写的一个记事本程序.我知道现在市面上有各种各样的记事本了,但是我发现有的写的功能不够完善,或者是代码层次结构不够清晰,有的甚至看了之后云里雾里的,有的还不乏了非常明显的 ...

  2. static 变量(静态变量)

    在C++的面向对象编程中,static还可以加在类的数据成员或成员函数之前.这样定义的数据成员或成员函数就被类所拥有,而不再属于类的对象. #include <iostream> usin ...

  3. SQL Server性能优化——等待——SLEEP_BPROOL_FLUSH

    前言: 有一个用于历史归档的数据库(简称历史库),经过一定时间的积累,数据文件已经达到700多GB,后来决定某些数据可以不需要保留,就把这部分数据truncate了,空余出600多GB的空间,也就是说 ...

  4. 开源负载均衡通讯分发器(LB dispatcher) - G5

    from:http://bbs.csdn.net/topics/390753043 1.开发背景今天和系统运维的老大聊天,谈到一直在用的F5,行里对其评价为价格过高.功能复杂难懂,反正印象不是很好,使 ...

  5. Linux系统编程---实现目录或者文件拷贝

    关于拷贝文件,前面写过一个例子:点击打开链接 ,可以看看,实现cp命令. 这次我们实现一个目录和文件的拷贝,综合点. #include <stdio.h> #include <fcn ...

  6. android布局##TableLayout和FrameLayout-android学习之旅(十五)

    TableLayout 表格布局 tablelayout简介 表格布局有TableLayout代表,但是它的本质定义仍然是线性管理器.表格布局采用行和列来管理UI,但是不需要明确的定义多少行,多少列, ...

  7. EBS开发技术之Patch安装

     Contents Document Control........................................................................ ...

  8. Activity, Service,Task, Process and Thread之间的关系

    Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...

  9. 纯CSS箭头,气泡

    原文地址: CSS Triangles 演示地址:CSS Triangles Demo 原文日期: 2013年8月5日 翻译日期: 2013年8月9日 本文两种实现方式: 使用或不使用 before ...

  10. iOS开发中 常用枚举和常用的一些运算符(易错总结)

    1.色值的随机值: #define kColorValue arc4random_uniform(256)/255.0 // arc4random_uniform(256)/255.0; 求出0.0~ ...