100 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.
Note: A leaf is a node with no children.
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.
S1:
/**
* 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;
if(!root->right && !root->left && root->val == sum) return true;
return(hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val));
}
};
100 Path Sum的更多相关文章
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- 【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 ...
- [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
随机推荐
- 【python】提取sql语句中的表名
前言 最近刚学python,写一个小工具时需要提取sql语句中表名,查询一番后找到一篇文章挺不错的,mark一下 PS.那篇文章是转载的,且没有标注转载自哪里 正文 import ply.lex as ...
- 【转】sublime text 2 快捷键大全
Ctrl+L 选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行( ...
- JVM 参数(转)
Herry灬凌夜 转自:https://www.cnblogs.com/wuyx/p/9627542.html 常用的JVM配置参数 一.Trace 跟踪参数 在Eclipse中,如何打开GC的监控 ...
- 998. 最大二叉树 II
最大树定义:一个树,其中每个节点的值都大于其子树中的任何其他值. 给出最大树的根节点 root. 就像之前的问题那样,给定的树是从表 A(root = Construct(A))递归地使用下述 Con ...
- connection closed by foreign host / Permissions 0620 for '/etc/ssh/ssh_host_ed25519_key' are too open 解决方案
发生此次故障的原因: 在文件夹授权时 错误的执行了 chmod -R 755 / 本来只想授权当前文件夹的 结果... 然后就导致xshell连不上了 懵逼... 解决方案 将权限收回: 执行: ch ...
- 《闲扯Redis一》五种数据类型之String型
一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...
- 在vscode中配置LeetCode插件,从此愉快地刷题
大家好,今早在B站看到up主的vscode里藏了leetcode插件,这才知道原来还有这款神器.但是没想到在用的时候遇到了一些麻烦,花了一点时间才解决.所以写这篇文章除了给大家安利这个好用的插件之外, ...
- 补充《解析“60k”大佬的19道C#面试题(上)》
[广州.NET技术俱乐部]微信群的周杰写了一篇文章<解析“60k”大佬的19道C#面试题(上)>https://www.cnblogs.com/sdflysha/p/20200325-19 ...
- CDNbest-设置不缓存
写在开始之前 有时候根据业务需求,我们网站不需要缓存,这时候就需要设置下 让网站不走缓存 步骤一 登录平台找到我们的站点->站点设置->缓存设置 如图 备注:填写需要操作的域名,时间为&q ...
- [模拟] Codefroces 1175B Catch Overflow!
题目:http://codeforces.com/contest/1175/problem/B B. Catch Overflow! time limit per test 1 second memo ...