当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢
14. Users, groups, and permissions (14)
The city chain technology Digital Innovation Strategy Summit was successfully held
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
leetcode刷题:二叉树13(相同的树)
Fundamentals of deep learning convolutional neural network (CNN)
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
No matter how busy you are, you can't forget safety
Wechat applet regular expression extraction link
随机推荐
Force buckle 729 My schedule I
Let's talk about threadlocalinsecurerandom
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
【无标题】
DP:树DP
MySql的root密码忘记该怎么找回
selenium 元素信息
ICTCLAS word Lucene 4.9 binding
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
C application interface development foundation - form control (5) - grouping control
leetcode刷题:二叉树16(路径总和)
解决php无法将string转换为json的办法
id选择器和类选择器的区别
Recommended collection, my Tencent Android interview experience sharing
Go language | 02 for loop and the use of common functions
Using repositoryprovider to simplify the value passing of parent-child components
使用 RepositoryProvider简化父子组件的传值
How to select the Block Editor? Impression notes verse, notation, flowus
Thread pool parameters and reasonable settings