LeetCode 112. 路径总和(Path Sum) 10
112. 路径总和
112. Path Sum
题目描述
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
每日一算法2019/5/13Day 10LeetCode112. Path Sum
示例:
给定如下二叉树,以及目标和 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
返回 true,因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
Java 实现
Recursive Solution
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public boolean hasPathSum(TreeNode root, int target) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null && root.val == target) {
return true;
}
return hasPathSum(root.left, target - root.val) || hasPathSum(root.right, target - root.val);
}
}
import java.util.ArrayList;
import java.util.List;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public boolean hasPathSum(TreeNode root, int target) {
List<Integer> answer = new ArrayList<>();
if (root == null) {
return false;
}
searchPath(root, 0, target, answer);
if (answer.contains(target)) {
return true;
}
return false;
}
private void searchPath(TreeNode root, int sum, int target, List<Integer> answer) {
if (root.left == null && root.right == null) {
answer.add(sum + root.val);
}
if (root.left != null) {
searchPath(root.left, sum + root.val, target, answer);
}
if (root.right != null) {
searchPath(root.right, sum + root.val, target, answer);
}
}
}
相似题目
参考资料
LeetCode 112. 路径总和(Path Sum) 10的更多相关文章
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- Java实现 LeetCode 112 路径总和
112. 路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标 ...
- [LeetCode] #112 #113 #437 Path Sum Series
首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...
- LeetCode 112. 路径总和(Path Sum)
题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...
- [Swift]LeetCode112. 路径总和 | Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112.路径总和(C++)
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...
- LeetCode 112. 路径总和 (递归遍历二叉树)
题目链接:https://leetcode-cn.com/problems/path-sum/ 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- LeetCode:路径总和【112】
LeetCode:路径总和[112] 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例 ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
随机推荐
- vuex(用了vue就上了一条不归路的贼船)
一.Vuex是干什么用的? 它是用于对复杂应用进行状态管理用的(官方说法是它是一种状态管理模式). “杀鸡不用宰牛刀”.对于简单的项目,根本用不着Vuex这把“宰牛刀”.那简单的项目用什么呢?用Vue ...
- beego-vue URL重定向(beego和vue前后端分离开发,beego承载vue前端分离页面部署)
具体过程就不说,是搞这个的自然会动,只把关键代码贴出来. beego和vue前后端分离开发,beego承载vue前端分离页面部署 // landv.cnblogs.com //没有授权转载我的内容,再 ...
- Latex 数字加粗后变宽 Latex bold without increasing the length of the text
Add the following code at the beginning of the article \newsavebox\CBox \def\textBF#1{\sbox\CBox{#1} ...
- 使用ListView应该注意的地方
android:clipToPadding和android:clipChildren http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/201 ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- Spark(五十):使用JvisualVM监控Spark Executor JVM
引导 Windows环境下JvisulaVM一般存在于安装了JDK的目录${JAVA_HOME}/bin/JvisualVM.exe,它支持(本地和远程)jstatd和JMX两种方式连接远程JVM. ...
- 数据缺失值的处理 | R包 - mice
有些情况下缺失值会零星的分布在数据当中,这时去掉所有包含缺失值的样本就不行了,直接用0去填补缺失值也不行. 所以此时就应该用拟合的方法来填补缺失值. library(mice) init = mice ...
- Vue 使用axios分片上传
Vue的界面 <input type="file"/> 上传方法 fileUpload: function () { var num = 1 var file = do ...
- K8S集群Master高可用实践
K8S集群Master高可用实践 https://blog.51cto.com/ylw6006/2164981 本文将在前文基础上介绍k8s集群的高可用实践,一般来讲,k8s集群高可用主要包含以 ...
- Leetcode: Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...