当前位置:网站首页>leetcode 112. Path sum recursion
leetcode 112. Path sum recursion
2022-08-08 04:04:00 【Alkali!】
topic description
Path sum: link to original title
Ideas
It can be solved recursively
- Encountered empty node: Since the tree is empty, there is no path from the root node to the leaf node, return false
- Currently leaf node: determine whether the sum of the current path is equal to targetSum
- Currently a general node: Recursively go to the left subtree and right subtree, and subtract the value of the current node from the target value
Code
class Solution {public:bool hasPathSum(TreeNode* root, int targetSum) {if(!root) return false; //Because the tree is empty, there is no path from the root node to the leaf node, and the boundary point is reachedif(!root->left&&!root->right) return targetSum==root->val; //To the leaf node, judge whether it meets the conditionsreturn hasPathSum(root->left,targetSum-root->val)||hasPathSum(root->right,targetSum-root->val); //The leaf node is not reached, recurse downward}}; 边栏推荐
- 32. 你知道Redis的字符串是怎么实现的吗?
- egg-阿里云短信配置
- egg-session stores data to redis
- The effect of base 0 or base 1 on the number of image iterations
- VSCode打开 C(嵌入式) 工程的一些记录
- MySQL from entry to entry [20W word collection]
- 剑指Offer 18.删除链表的节点
- Hangzhou Electric Multi-School 6 1009. Map
- JS 怎么使用十六进制保存100位状态的问题
- 响应式pbootcms模板健身器械类网站
猜你喜欢

121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks

以0为底或以1为底对图片迭代次数的影响

Vulfocus Shooting Range Scenario Mode - Intranet Dead End
![MySQL from entry to entry [20W word collection]](/img/da/1a40e831aa541001773bb76fd369f4.png)
MySQL from entry to entry [20W word collection]

egg-Alibaba Cloud SMS Configuration

vulnhub-DC-5靶机渗透记录

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model

06 tp6 的数据更新(改)及删除 《ThinkPHP6 入门到电商实战》

The difference between orElse and orElseGet in Optional

NorFlash的存储原理
随机推荐
国内最主流的5大项目工时管理系统
Typical Data Warehouse Modeling Methodology
数据库缓冲区高速缓存中块的类型
From hardcover to smartcover, the next wave is emerging. Listen to what smart home gurus have to say?
农产品直播带货持续升温,经济日报:冲流量勿忘质量
STFW3N150 Pin Function Datasheet (PDF) Pin Diagram
egg-session 将数据存储到redis
leetcode 112.路经总和 递归
mfc对话框添加OnMouseMove
剑指 Offer 17. 打印从1到最大的n位数
Hangzhou Electric Multi-School 6 1009. Map
NorFlash的存储原理
10款自媒体人必备的免费工具,快速高效运营
Knowledge of DisplayPort-DP interface
项目分析(嵌入式产品Web化)
Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
强网杯 2019-随便注 (堆叠注入)
【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
高薪程序员&面试题精讲系列134之微服务网关有哪些限流算法?如何实现限流?
C语言 扫雷