算法:Path Sum(路径总和)
说明
算法: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(路径总和)相关推荐
- 112. Path Sum 路径总和
Title 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...
- 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 ...
- 49 - 算法 - Leetcode 112 路径总和 -二叉树递归 带参数一起递归
// 感觉都是那几个固定的格式 寻找临时 单前树 状态输入减小 int sum递减 //树递归 所带的参数也递归减少class Solution {public:bool hasPathSum(Tre ...
- leetcode题库--112路径总和
路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 思路: 思路比较简单,就是递归将前面的和传 ...
- leetcode 113. 路径总和 II(Path Sum II)
目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...
- [Swift]LeetCode113. 路径总和 II | Path Sum II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 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/ 题解 简单的遍历二叉树,不解 ...
- 113. 路径总和 (剑指 Offer 34. 二叉树中和为某一值的路径)(回溯算法)
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径. 叶子节点 是指没有子节点的节点. 示例 1: 输入:root = [ ...
- LeetCode-二叉树算法总结-层次遍历,路径总和等
版权声明:本文为博主原创文章,欢迎转载,但请注明出处,谢谢愿意分享知识的你~~ https://blog.csdn.net/qq_32690999/article/details/80484440 & ...
- [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 ...
最新文章
- springboot的笔记
- Cadence 建立封装:多个引脚于芯片内部连接的封装建立方式
- 学习性代码和使用不存在的代码
- 2009年北京突然的一场雪
- C++中的空类,默认产生哪些类成员函数?
- swagger 使用指南
- C++虚继承中构造函数和析构函数顺序问题以及原理
- java main方法背后的故事?(转)
- 文字在状态栏上从右往左显示,而且是循环的
- 面试官不讲武德,竟然问了我18个JVM问题!
- 4090万美元成交!马斯克又卖出三处住宅以兑现“无房产”诺言
- ::-webkit-scrollbar 滚动条的设置
- java中reg=new,JavaScript replace new RegExp使用介绍
- 矩阵理论 期末判断题汇总
- 动态规划处理长方形分割为正方形问题
- Flutter 鼠标右键
- 杭州地铁行业十四五发展可行性及投资机遇研究报告2022版
- Hadoop集群能打开50070端口网页不能打开8088端口网页
- PTA1003 python3
- 非华为电脑多屏协同安装最新的电脑管家
热门文章
- c语言中函数的递归调用,用C语言函数调用与递归解决问题
- 如何添加、管理和删除PDF高亮文本?
- angular1的复选框指令--checklistModel
- Firefox 火狐恢复下载失败的任务(原创)
- webpack 生产环境下插件用途
- 第十章 Scala 容器基础(二十二):合并有序集合
- 编程珠玑:位图法排序
- 网易家居专访柯拉尼陶晓松:有所为 有所不为 争创领军品牌
- 大连理工计算机基础作业2,大工18秋《计算机应用基础》在线测试2答案
- eclipse报错Server Tomcat v7.0 Server at localhost failed to start.