Path Sum

OJ: https://oj.leetcode.com/problems/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.

思想: 先序遍历。若当前结点为空,返回 false; 不为空,则加上其值,若为叶子结点,则判断一次。

注意: 非路径和, 而是到叶子结点的路径和。例如: {1, 2} 1  返回: false

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool judge(TreeNode *root, int curSum, int& sum) {
if(root == NULL) return false;
curSum += root->val;
if(!root->left && !root->right) return curSum == sum;
return judge(root->left, curSum, sum) || judge(root->right, curSum, sum);
}
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
return judge(root, 0, 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]
]

思路同上: 只是要记下路径。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void getPath(TreeNode *root, int curSum, int& sum, vector<vector<int> > &pathSet, vector<int> &path) {
if(root == NULL) return;
curSum += root->val;
path.push_back(root->val);
if(!root->left && !root->right && curSum == sum)
pathSet.push_back(path);
getPath(root->left, curSum, sum, pathSet, path);
getPath(root->right, curSum, sum, pathSet, path);
path.pop_back();
}
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> >pathSet;
vector<int> path;
getPath(root, 0, sum, pathSet, path);
return pathSet;
}
};

32. Path Sum && Path Sum II的更多相关文章

  1. Path Sum,Path Sum II

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

  2. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  3. 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 ...

  4. 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) ...

  5. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  6. 关于数论分块里r=sum/(sum/l)的证明!

    今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表 ...

  7. 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。

    今天在浏览网页的时候,发现了一个叫做  华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...

  8. 有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。

    有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为    A = sum(a) - ...

  9. os.path.exists(path) 和 os.path.lexists(path) 的区别

    使用os.path.exists()方法可以直接判断文件是否存在.代码如下:>>> import os>>> os.path.exists(r'C:\1.TXT') ...

随机推荐

  1. Codeforces Round #379 (Div. 2) 解题报告

    题目地址 本次CF是在今天早上深夜进行,上午有课就没有直接参加.今天早上上课坐到后排参加了virtual participation.这次CF前面的题目都非常的水,不到10分钟就轻松过了前两题,比较郁 ...

  2. 初识selendroid

    Testerhome社区的lihuazhang对selendroid官网的部分内容进行了翻译和讲解. 以下内容均摘自lihuazhang.感谢lihuazhang的讲解.原文地址:https://gi ...

  3. Apache+Tomcat构建Tomcat负载均衡集群

    一.环境介绍 二.安装后端服务器 三.安装前端Apache服务 四.配置Apache使用mod_jk模块实现代理及负载均衡 五.配置Apache基于mod_proxy模块实现代理及负载均衡 六.论坛安 ...

  4. sqoop的merge和eval 工具

    1.sqoop的merge的工具 sqoop merge 可以将hdfs上的两个文件进行合并,在increment import的过程中经常会用到,如incremenet import将数据导入到hd ...

  5. Debian 8(jessie)下设置系统启动直接进入命令行,无GUI

    修改grub项 sudo vi /etc/default/grub 修改其中三项 ... GRUB_CMDLINE_LINUX_DEFAULT="quiet" GRUB_CMDLI ...

  6. SAP 增强-出口选找方法-全部

    ■ SAP 中如何寻找增强 方法一:利用TCODE寻找增强(第二代的增强) 执行一个程序(源代码后附),在选择屏幕处输入你所需要增强的程序TCODE,执行後,就会出现一个列表,那里就有关于如何增强这个 ...

  7. SqlServer性能优化:创建性能监视器(二)

    添加三个选项: 下一步就可以了 Sql跟踪的模板: 跟踪Sql 语句的执行情况: 使用刚才的新建的模板: 用到的Sql语句: select * from [Sales].[SalesOrderDeta ...

  8. mysql取代rand()的高效率随机读取方法

    SELECT * FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `table`)) AS id) AS t2 ...

  9. Error:Execution failed for task ':app:transformClassesWithInstantRunForDebug'. > java.lang.ClassNotFoundException: io.realm.RealmObject

    这问题错误是在运行时出现的,A-Jiang大神有说明解决方法,大神网址:https://github.com/AndroidJiang/StockChart 如果是as2.0+,可能会出现instan ...

  10. JAVA中精确计算金额BigDecimal

    package com.chauvet.utils; import java.math.BigDecimal; import java.text.DecimalFormat; import java. ...