当前位置:网站首页>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));
}
}

边栏推荐
- A solution to PHP's inability to convert strings into JSON
- What is PyC file
- Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
- Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
- -v parameter of GST launch
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- leetcode刷题:二叉树14(左叶子之和)
- c——顺序结构
- Go language learning tutorial (16)
- 解决php无法将string转换为json的办法
猜你喜欢

Wechat applet regular expression extraction link

14. Users, groups, and permissions (14)

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

How to safely and quickly migrate from CentOS to openeuler

leetcode刷题:二叉树14(左叶子之和)

图嵌入Graph embedding学习笔记

Zero cloud new UI design

Using repositoryprovider to simplify the value passing of parent-child components

【无标题】

Force buckle 729 My schedule I
随机推荐
How to apply smart contracts more wisely in 2022?
leetcode刷题:二叉树15(找树左下角的值)
Wildcard selector
SecureRandom那些事|真伪随机数
Debezium series: modify the source code to support drop foreign key if exists FK
Force buckle 1200 Minimum absolute difference
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
JVMRandom不可设置种子|问题追溯|源码追溯
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
浅浅的谈一下ThreadLocalInsecureRandom
如何安全快速地从 Centos迁移到openEuler
Is the education of caiqiantang reliable and safe?
Go language learning tutorial (XV)
Leetcode brush questions: binary tree 11 (balanced binary tree)
Add data to excel small and medium-sized cases through poi
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
How to retrieve the root password of MySQL if you forget it
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl