当前位置:网站首页>Leetcode 112: path sum
Leetcode 112: path sum
2022-07-29 01:49:00 【Swarford】
subject :
Ideas :dfs
The idea is similar to Change for , Every time you recurse down , Add up the number you want sum Subtract the current node root Of val, In this way, we can get the money we need to collect from the next layer , Until the leaf node sum Node equal val Return true !
The topic is about root node to leaf node ! So we have to traverse to the end , Use post order traversal ;
Single layer node : Determine whether it is currently a leaf node , Yes, judge sum and val Whether it is equal or not ; If it's not a leaf node , Continue to recursive ;
base case: If root==null ,return false;
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
// You must go to the leaf node !
// You must traverse to the end ! dfs
// Traversing to the leaf node is not equal to sum, Then return to false;
if(root==null){
return false;
}
// Catch the result , to flash back
boolean left=hasPathSum(root.left,sum-root.val);
boolean right=hasPathSum(root.right,sum-root.val);
// Current node :
// To the left and right null Is the leaf node !
if(root.left==null && root.right==null){
// Traverse to the end , here root value =sum To return to true !
return root.val==sum;
}
return left || right;
}
}
边栏推荐
- Platofarm community ecological gospel, users can get premium income with elephant swap
- JVM learning minutes
- Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
- AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
- [hcip] two mGRE networks are interconnected through OSPF (ENSP)
- Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
- 【golang】使用select {}
- JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
- Network security litigation risk: four issues that chief information security officers are most concerned about
- [unity project practice] synthetic watermelon
猜你喜欢

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?

【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索

Platofarm community ecological gospel, users can get premium income with elephant swap

MPEG音频编码三十年

T-sne降维
![[hcip] two mGRE networks are interconnected through OSPF (ENSP)](/img/fe/8bb51ac48f52d61e8d31af490300bb.png)
[hcip] two mGRE networks are interconnected through OSPF (ENSP)

【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”

golang run时报undefined错误【已解决】

What are the common cyber threats faced by manufacturers and how do they protect themselves

明日无限计划,2022某公司元宇宙产品发布会活动概念策划方案
随机推荐
The new generation of public chain attacks the "Impossible Triangle"
规划数学期末考试模拟二
[hcip] experiment of republishing and routing strategy
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
Google Cloud Spanner的实践经验
BOM系列之window对象
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
[search] - DFS pruning and optimization
What is the ISO assessment? How to do the waiting insurance scheme
Openpyxl border
LeetCode 113:路径总和 II
mysql的执行顺序
Lombook User Guide
Internship: tool class writing for type judgment
[hcip] MPLS Foundation
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
With the explosive growth of digital identity in 2022, global organizations are facing greater network security
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
Reinforcement learning (II): SARS, with code rewriting
body中基本标签