当前位置:网站首页>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));
}
}
边栏推荐
- MMO project learning 1: preheating
- How to safely and quickly migrate from CentOS to openeuler
- 打新债在哪里操作开户是更安全可靠的呢
- Fundamentals of shell programming (Part 8: branch statements -case in)
- Which securities company is better and which platform is safer for mobile account opening
- id选择器和类选择器的区别
- SecureRandom那些事|真伪随机数
- [OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
- PHP uses ueditor to upload pictures and add watermarks
- S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
猜你喜欢
acm入门day1
安卓面试宝典,2022Android面试笔试总结
软件测试是干什么的?学习有啥要求?
Recommended collection, my Tencent Android interview experience sharing
不愧是大佬,字节大牛耗时八个月又一力作
C#应用程序界面开发基础——窗体控制(5)——分组类控件
Do you know several assertion methods commonly used by JMeter?
建议收藏,我的腾讯Android面试经历分享
No matter how busy you are, you can't forget safety
Android interview, Android audio and video development
随机推荐
Is it safe to open a mobile stock account? Is it reliable?
如何在2022年更明智地应用智能合约?
多分支结构
Android interview, Android audio and video development
id选择器和类选择器的区别
Hiengine: comparable to the local cloud native memory database engine
Common - Hero Minesweeper
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
Fundamentals of deep learning convolutional neural network (CNN)
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Thread pool parameters and reasonable settings
Reptile exercises (II)
What are general items
[AI framework basic technology] automatic derivation mechanism (autograd)
PG basics -- Logical Structure Management (user and permission management)
Xaas trap: all things serve (possible) is not what it really needs
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
城链科技数字化创新战略峰会圆满召开