当前位置:网站首页>Leetcode skimming: binary tree 16 (path sum)
Leetcode skimming: binary tree 16 (path sum)
2022-07-05 20:03:00 【Taotao can't learn English】
112. The sum of the paths
Given a binary tree and a target and , Determine whether there is a path from root node to leaf node in the tree , The sum of the values of all nodes in this path is equal to the target and .
explain : A leaf node is a node that has no children .
Example :
Given the following binary tree , And goals and sum = 22,

return true, Because there are goals and for 22 The path from the root node to the leaf node 5->4->11->2.
Recursive traversal , If there are only left trees , Then only the left subtree is calculated , If only the right subtree , Then only the right subtree is calculated , If you encounter an empty node, start comparing , Put the recursive result or operation that will do .
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. The sum of the paths **/
public class HasPathSum {
/** * Give you the root node of the binary tree root And an integer representing the sum of goals targetSum . Determine if there is Root node to leaf node The path of , The sum of the values of all nodes in this path is equal to the target and targetSum . If there is , return true ; otherwise , return false . * Leaf node A node without children . * <p> * .. It's just to traverse every path and * * @param root * @param targetSum * @return */
public boolean hasPathSum(TreeNode root, int targetSum) {
// Using recursion
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;
}
// Calculate only to leaf nodes
sum += root.val;
// Only the result of right subtree is calculated
if (root.left == null && root.right != null) {
return traversal(root.right, sum, targetSum);
}
// Only calculate the result of the left subtree
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));
}
}

边栏推荐
- 国信证券在网上开户安全吗?
- Wechat applet regular expression extraction link
- Go language | 01 wsl+vscode environment construction pit avoidance Guide
- [C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
- BZOJ 3747 POI2015 Kinoman 段树
- 【c语言】快速排序的三种实现以及优化细节
- How to retrieve the root password of MySQL if you forget it
- 城链科技数字化创新战略峰会圆满召开
- 港股将迎“最牛十元店“,名创优品能借IPO突围?
- MySql的root密码忘记该怎么找回
猜你喜欢

JVMRandom不可设置种子|问题追溯|源码追溯

实操演示:产研团队如何高效构建需求工作流?

95后阿里P7晒出工资单:狠补了这个,真香...

Leetcode: binary tree 15 (find the value in the lower left corner of the tree)

Leetcode brush questions: binary tree 11 (balanced binary tree)

微信小程序正则表达式提取链接
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

.Net分布式事務及落地解决方案

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

Webuploader file upload drag upload progress monitoring type control upload result monitoring control
随机推荐
如何安全快速地从 Centos迁移到openEuler
Analysis of openh264 decoded data flow
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
Force buckle 729 My schedule I
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
leetcode刷题:二叉树18(最大二叉树)
JVMRandom不可设置种子|问题追溯|源码追溯
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
C langue OJ obtenir PE, ACM démarrer OJ
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Parler de threadlocal insecurerandom
. Net distributed transaction and landing solution
股票开户哪里好?网上客户经理开户安全吗
淺淺的談一下ThreadLocalInsecureRandom
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
安信证券在网上开户安全吗?
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom