题目

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.

解题思路

递归解法,sum减去根节点的值,再依次判断是否和叶子节点是否相等,相等则返回True。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def hasPathSum(self, root: TreeNode, sum: int) -> bool:if root is None:return Falseif not root.left and not root.right and root.val == sum:return Truesum -= root.valreturn self.hasPathSum(root.left, sum) or self.hasPathSum(root.right, sum)

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. gdb调试时查看内存
  2. hdu 5312 数学
  3. 云+X案例展 | 民生类:纷享销客助力沃得农机构筑智能化、信息化之路
  4. AI取代人类?这4种工作仍将是从业者的“铁饭碗” | 未来
  5. undefined reference to `clock_gettime'
  6. SQL ---- 查询字段为逗号拼接的字符串
  7. ORA-01033 ORACLE 正在初始化或关闭
  8. 主成分分析(PCA)实现代码
  9. 负负为何得正的推理过程
  10. python3标识符_python3学习笔记一(标识符、关键字)
  11. 语义替换脚本——论文降重GUI
  12. 电信吉比特光纤猫虚拟服务器设置,光猫设置教程 华为光猫设置教程
  13. 电子商业汇票知识问答题
  14. 计算机小高考成绩,2018江苏小高考成绩出来了!昆山*亮眼的学校是…
  15. 概率论得学习整理--番外3:二项式定理和 二项式系数
  16. 汤姆猫代码python_用树莓派实现会说话的汤姆猫
  17. C++调用ffmpeg批量合并bilibili缓存视频
  18. iOS动画:3D动画(18)
  19. Linux keypad 设备树,GitHub - bilibilifmk/rpi_mt: 树莓派移动终端 全键盘及2.4寸tft材质屏幕...
  20. JAVA IO必会概念

热门文章

  1. SpringBoot之前端文件管理
  2. 当年修复Linux启动菜单的笔记
  3. 使用CTE替换派生表语法
  4. Expression Tree 上手指南 (二)
  5. php 调用url field,使用 PHP curl 调用 API,如何传递如下格式的 POSTFIELD?
  6. wes 7 gost 下载_南京地铁实现移动5G全覆盖下载速度快到飞起_机房360
  7. pandas中的reset_index()
  8. 独家|华为进行组织架构调整 CloudAI升至第四大BG
  9. Java接口和Java抽象类
  10. 直播|微软徐一翀:在常识问答中超越人类水平!自注意力与外注意力机制