题目:
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.

思路:
根节点到叶子节点的路径的所有值如果有与sum一样的,则返回true;否则返回false。
如果root为NULL,直接返回false;如果根节点无子叶节点 ,且根节点的值和sum相等,返回true;递归调用,分别判断左节点和右节点,如果有一个满足情况的,就返回为true。

代码:

/*** 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==NULL){//如果root为NULL,直接返回falsereturn false;}if(!(root->left||root->right)&&root->val==sum){//如果根节点无子叶节点 ,且根节点的值和sum相等,返回truereturn true;}//递归调用,分别判断左节点和右节点,如果有一个满足情况的,就返回为truereturn hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);}
};

输出结果: 12ms

LeetCode 112. Path Sum相关推荐

  1. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  2. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  3. Leetcode: 112. Path Sum

    题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

  5. [leetcode] Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  6. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

  7. leetcode python3 简单题112. Path Sum

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十二题 (1)题目 英文: Given a binary tree and ...

  8. Leetcode: 113. Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode Minimum Path Sum(动态规划)

     Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right wh ...

最新文章

  1. 前四次作业--个人总结
  2. WPF与Win32互操作
  3. 对传入的值,转成整数
  4. Logtail提升采集性能
  5. 为什么拼多多没有购物车?
  6. 希望找到计算机系的好期刊的影响因子的排序?并作为选刊依据?选SJR就足够了
  7. [Java开发]打印当前路径到控制台
  8. “约见”面试官系列之常见面试题之第一百零七篇之vue的作用(建议收藏)
  9. System.Web.HttpContext.Current.Session为NULL解决方法
  10. 云+X案例展 | 金融类:荣之联助力君康人寿构建新一代数据中心
  11. 数组长度属性以及JavaScript中的示例
  12. java的数列极差_[hoj]数列极差问题 | 学步园
  13. 遥感技术在水利行业中的应用
  14. 1.2 批量生成MySQL建表语句
  15. VMware ESXi 6.7安装过程介绍
  16. 尚德机构推出2019版CPA系列纸质书 配套小程序刷题
  17. MySQL燕十八老师课程笔记:第十六课:MySQL各个函数
  18. 点计算机图标就自动关机了,为什么点计算机的关机图标不能自动关机
  19. 微信OAuth授权获取用户OpenId-JAVA(个人经验)
  20. 自然辩证法复习题 1

热门文章

  1. php mysql 日期查找_PHP / MySQL存储和搜索日期
  2. C++:new[]和delete[]一定要配对使用吗?
  3. OpenGL:读取图片显示
  4. Scala里Map()集合
  5. 前端框架:AntdUI 文档入门
  6. mui框架mui-active高亮当前栏目 - 代码说明
  7. 关于多个router-view视图组件,渲染同一页面
  8. IE浏览器兼容性处理与提示
  9. 苏州大学计算机组成与结构,苏州大学计算机组成结构期末.docx
  10. python怎么播放本地录音_Python播放音频与录音