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的更多相关文章

  1. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  2. [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 ...

  3. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  4. LeetCode 931. Minimum Falling Path Sum

    原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...

  5. [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 ...

  6. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

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

  8. [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 ...

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

随机推荐

  1. JVM笔记-运行时内存区域划分

    1. 概述 Java 虚拟机在执行 Java 程序的过程中会把它管理的内存划分为若干个不同的数据区域.它们各有用途,有些随着虚拟机进程的启动一直存在(堆.方法区),有些则随着用户线程的启动和结束而建立 ...

  2. S3C2440A特殊寄存器

    S3C2440A特殊寄存器 特殊寄存器有: 输入输出端口 存储器控制器 NANDFLASH 看门狗定时器 时钟和电源管理 PWM定时器 UART USB设备 中断控制器 DMA LCD控制器 RTC ...

  3. 初创电商公司Drop的数据湖实践

    欢迎关注微信公众号:ApacheHudi 1. 引入 Drop是一个智能的奖励平台,旨在通过奖励会员在他们喜爱的品牌购物时获得的Drop积分来提升会员的生活,同时帮助他们发现与他们生活方式产生共鸣的新 ...

  4. 创建Sphinx + GitHub + ReadtheDocs托管文档

    最新博客链接 "Tsanfer's Blog" 创建Sphinx + GitHub + ReadtheDocs托管文档 Readthedocs在线电子书链接

  5. 《Python学习手册 第五版》 -第15章 文档

    本章主要介绍Python中的文档,会通过多种方式来说明,如果查看Python自带文档和其他参考的资料 本章重点内容 1.#注释:源文件文档 2.dir函数:以列表显示对象中可用的属性 3.文档字符串 ...

  6. 039.集群网络-Pod和SVC网络实践

    一 Pod和SVC网络 1.1 实践准备及原理 Docker实现了不同的网络模式,Kubernetes也以一种不同的方式来解决这些网络模式的挑战.本完整实验深入剖析Kubernetes在网络层是如何实 ...

  7. 如何区分前端BUG和后台BUG?

    测试工程师不只是负责发现问题,除了发现问题这种基本功外,定位问题,提出解决方案,提出预防方案也是要掌握的技能.这里先说定位问题的要求,定位问题要向深入,前提当然是对功能.产品的流程.开发方案.开发人员 ...

  8. 【西北大学集训队选拔赛】D温暖的签到题(自创数据结构)

    题目链接 #include <bits/stdc++.h> #define NUM #define ll long long using namespace std; int n, m; ...

  9. 北邮OJ-257- 最近公共祖先-软件14 java

    思路分析:思路应该比较简单也很容易想的来,就是比较两个节点的最近的祖先节点,要对每个节点依次记录下他的所有祖先节点,包括其自己,因为自己也算自己的祖先节点,这一点题目中没有明确指出 所以比较坑. 我们 ...

  10. [leetcode] 位操作题解

    子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [ ...