当前位置:网站首页>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}}; 边栏推荐
- The project management process and key points for each link
- PC Museum (Fanwai 01)-Chenghuiwan, junior high school students develop a large-scale navigation game with physical scales
- Lecture 84th Biweekly
- Monitoring tool Prometheus and project summary, 220805,,
- VSCode打开 C(嵌入式) 工程的一些记录
- ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
- The sword refers to Offer 17. Print the n digits from 1 to the largest
- 6G时代新用户面设计和关键技术
- 中国科学院金属研究所科研课题获华为技术认证,助力材料学发展新范式!
- 云原生(三十三) | Kubernetes篇之平台存储系统部署
猜你喜欢
随机推荐
fail-fast 和 fail-safe 快速学习
ICML 2022 | LeNSE: Subgraph-Based Large-Scale Combinatorial Optimization: Achieving Over 140x Speedup
面向6G的通信感知一体化架构与关键技术
亚马逊云科技Build On学习心得
y90.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(一)
响应式pbootcms模板健身器械类网站
egg-validate-custom validation method error language (error Chinese prompt)
leetcode: 122. 买卖股票的最佳时机 II
The storage principle of NorFlash
基于MindSpore框架的数字调制信号盲识别研究
云原生(三十三) | Kubernetes篇之平台存储系统部署
unity之粒子特效制作图片拼合文字效果
121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks
数据在内存如何分布的?
Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
The effect of base 0 or base 1 on the number of image iterations
Data labeling platform doccano----Introduction, installation, use, pit record
农产品直播带货持续升温,经济日报:冲流量勿忘质量
让你的文字被更多人看到:来投稿吧,稿酬靠谱!
PC Museum (5) 1975 IMSAI 8080









