当前位置:网站首页>leetcode刷题:二叉树16(路径总和)
leetcode刷题:二叉树16(路径总和)
2022-07-05 19:52:00 【涛涛英语学不进去】
112. 路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22,
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
递归遍历,如果只有左子树,则只计算左子树,如果只有右子树,则只计算右子树,遇到空节点则开始比较,把递归结果 或 运算 即可。
package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
import java.util.Stack;
/** * @ClassName HasPathSum * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 11:40 * @Version 1.0 * https://leetcode.cn/problems/path-sum/ * 112. 路径总和 **/
public class HasPathSum {
/** * 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。 * 叶子节点 是指没有子节点的节点。 * <p> * ..不就是遍历每条路径和嘛 * * @param root * @param targetSum * @return */
public boolean hasPathSum(TreeNode root, int targetSum) {
//使用递归
if (root == null) {
return false;
}
return traversal(root, 0, targetSum);
}
public boolean traversal(TreeNode root, int sum, int targetSum) {
if (root == null) {
return sum == targetSum;
}
//只计算到叶子结点
sum += root.val;
//只计算右子树的结果
if (root.left == null && root.right != null) {
return traversal(root.right, sum, targetSum);
}
//只计算左子树的结果
if (root.left != null && root.right == null) {
return traversal(root.left, sum, targetSum);
}
return traversal(root.left, sum, targetSum) || traversal(root.right, sum, targetSum);
}
public static void main(String[] args) {
System.out.println(new HasPathSum().hasPathSum(GenerateTreeNode.generateTreeNode("[5,4,8,11,null,13,4,7,2,null,null,null,1]"), 22));
}
}
边栏推荐
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- Xaas trap: all things serve (possible) is not what it really needs
- What are general items
- Reptile exercises (II)
- 完爆面试官,一线互联网企业高级Android工程师面试题大全
- Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
- 如何在2022年更明智地应用智能合约?
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
- What is the core value of testing?
- 建立自己的网站(16)
猜你喜欢
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
Add data to excel small and medium-sized cases through poi
Postman core function analysis - parameterization and test report
成功入职百度月薪35K,2022Android开发面试解答
Securerandom things | true and false random numbers
MMO项目学习一:预热
众昂矿业:2022年全球萤石行业市场供给现状分析
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
How MySQL queries and modifies JSON data
随机推荐
MMO项目学习一:预热
C - sequential structure
C application interface development foundation - form control (5) - grouping control
通过POI追加数据到excel中小案例
Fundamentals of shell programming (Part 8: branch statements -case in)
C#应用程序界面开发基础——窗体控制(5)——分组类控件
完爆面试官,一线互联网企业高级Android工程师面试题大全
Debezium series: parsing the default value character set
JMeter 常用的几种断言方法,你会了吗?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Multi branch structure
浅浅的谈一下ThreadLocalInsecureRandom
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
集合
Wildcard selector
不愧是大佬,字节大牛耗时八个月又一力作
Relationship between floating elements and parent and brother boxes
面试官:Redis中集合数据类型的内部实现方式是什么?
Android interview classic, 2022 Android interview written examination summary
深度学习 卷积神经网络(CNN)基础