说明

算法:Path Sum
LeetCode地址:https://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.

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 = sum - root.val , 问题就变成求左子树,或者右子树的和。则又是用到递归思想,子问题跟母问题的求解一样。

代码实现

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public boolean hasPathSum(TreeNode root, int sum) {if (root == null) {return false;}if (root.left == null && root.right == null && root.val == sum) return true;return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);}
}

代码执行效率

Runtime: 0 ms, faster than 100.00% of Java online submissions for Path Sum.
Memory Usage: 38.8 MB, less than 19.75% of Java online submissions for Path Sum.

总结

求路径总和问题,实则考察递归的应用。

代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/PathSum.java

算法:Path Sum(路径总和)相关推荐

  1. 112. Path Sum 路径总和

    Title 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...

  2. 64. Minimum Path Sum 路径最小总和

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

  3. 49 - 算法 - Leetcode 112 路径总和 -二叉树递归 带参数一起递归

    // 感觉都是那几个固定的格式 寻找临时 单前树 状态输入减小 int sum递减 //树递归 所带的参数也递归减少class Solution {public:bool hasPathSum(Tre ...

  4. leetcode题库--112路径总和

    路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 思路: 思路比较简单,就是递归将前面的和传 ...

  5. leetcode 113. 路径总和 II(Path Sum II)

    目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...

  6. [Swift]LeetCode113. 路径总和 II | Path Sum II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. 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/ 题解 简单的遍历二叉树,不解 ...

  8. 113. 路径总和 (剑指 Offer 34. 二叉树中和为某一值的路径)(回溯算法)

    给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径. 叶子节点 是指没有子节点的节点. 示例 1: 输入:root = [ ...

  9. LeetCode-二叉树算法总结-层次遍历,路径总和等

    版权声明:本文为博主原创文章,欢迎转载,但请注明出处,谢谢愿意分享知识的你~~ https://blog.csdn.net/qq_32690999/article/details/80484440 & ...

  10. [Leetcode] Path Sum II路径和

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

最新文章

  1. 在3ds Max中使用V-Ray 5渲染引擎视频教程
  2. ⑨③-不能浪费拥有的年轻资本
  3. js node 打包mac应用_混搭 TypeScript + GraphQL + DI + Decorator 风格写 Node.js 应用
  4. 安卓加载mysql数据到列表里_Android如何从数据库中加载海量数据
  5. wxWidgets:最小的 wxWidgets 示例
  6. 一步步学习 SAP CDS view Text Association 在 SAP Fiori Elements 中的应用
  7. 用PHP去掉文件头的Unicode签名(BOM)
  8. java 一一对应的替换_SpringMVC的Controller是如何将参数和前端传来的数据一一对应的...
  9. 最小生成树,回忆复习篇。
  10. 小学steam计算机课程案例,STEAM课程典型案例——桥世界
  11. 【ios】为什么要在Other Linker Flags添加Flag (eg:-ObjC、-lc++等)?
  12. android 手机安装windows7,手机怎么安装win7系统 安卓手机装win7系统教程
  13. HR不排斥的三大跳槽理由
  14. 哪些外贸软件比较好用?常用外贸软件有哪些?
  15. Loading 加载数据一直转圈圈停不下来怎么办,看这个一学就会
  16. matlab 将txt导入excel,[转载]MATLAB  批量导入excel和txt文件的方法
  17. html和css制作上三角和下三角
  18. Ubuntu 使用技巧【不断更新】
  19. Citrix 桌面云 XenApp_XenDesktop_7.15 部署系列(四)安装Delivery Controller和许可证服务器
  20. Machine Learning -- 揭开机器学习的面纱

热门文章

  1. python包管理工具pip_pip_python包管理工具(pip)下载 v9.0.1官方版 - 121下载站
  2. 安兔兔html5测试35000,安兔兔评测 8.4.3 安卓版
  3. Java—读取指定路径下文件的内容
  4. Scrapy保存中文字符到json文件时编码设置
  5. WPF触发器(Trigger、DataTrigger、EventTrigger)
  6. Android-path类整理
  7. 开启进程的两种方式:类、函数
  8. 硅谷初创企业控制成本 裁员风渐起
  9. C# EF6 更新对象
  10. 《WinForm开发系列之控件篇》Item65 VScrollBar (暂无)