当前位置:网站首页>Leetcode 112. path sum
Leetcode 112. path sum
2022-07-24 11:20:00 【LuZhouShiLi】
Leetcode 112. The sum of the paths
subject
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 .
Ideas
- Use the method of preorder traversal
- Determine the parameters and return values of recursive functions : The root node of the binary tree and a counter , The counter is used to calculate whether the node on a path of the binary tree is equal to the target value
- Determine termination conditions : Initialize the counter to the target value , Traverse one node at a time , Just subtract the node value , If the current node is a leaf node and count = 0, It shows that such a path has been found . If the leaf node is found, but count != 0, It indicates that this path does not conform to , Prepare to go back .
- Single layer recursive logic : Recursively traverses the left subtree , Recursively traverses the right subtree , First count Subtract the value of the node , If it doesn't , Directly rollback the node value .
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
bool traversakl(TreeNode* cur,int count)
{
// A leaf node is encountered and count = 0
if(!cur->left && !cur->right && count == 0)
{
return true;// Recursive export
}
if(!cur->left && !cur->right)
{
// Leaf node encountered also count != 0 It indicates that this path does not conform to
return false;// Recursive export
}
if(cur->left != NULL)
{
count -= cur->left->val;// Recursive left subtree
if(traversakl(cur->left,count))
{
return true;
}
count += cur->left->val;// to flash back Cancel processing result
}
if(cur->right != NULL)
{
count -= cur->right->val;
if(traversakl(cur->right,count))
{
return true;
}
count += cur->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == NULL)
{
return false;
}
return traversakl(root,targetSum - root->val);
}
};
边栏推荐
- Zynq TTC usage
- Selenium automated test (this one is enough) - self study
- CSDN blog removes the uploaded image watermark
- [golang] golang implements the post request to send form type data function
- Nodejs ctf 基础
- Blue Bridge Cup provincial match training camp - Calculation of date
- MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
- [golang] before method of time type in golang
- This should be postman, the most complete interface testing tool in the whole network
- MySQL根据备注查询表、字段
猜你喜欢

Use Modelsim to independently simulate Altera and Xilinx IP cores

Value and technical thinking of vectorization engine for HTAP

Idea background image set

Reprint of illustrations in nature, issue 3 - area map (part2-100)

Publish local image to private library

【10】 Teamwork and cross team collaboration

Depth first search and breadth first search of Graphs

聊聊软件测试-自动化测试框架

【C】 Recursive and non recursive writing of binary tree traversal

Text message verification of web crawler
随机推荐
STM32+ESP8266+MQTT协议连接阿里云物联网平台
Fifty lectures of Euler (I)
View the source code of idea Download
08【AIO编程】
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
Code of login page
This should be postman, the most complete interface testing tool in the whole network
The solution of permission denied
Xilinx FPGA Microblaze AXI_ IIC usage and experience
2022,软测人的平均薪资,看完我瞬间凉了...
Jmeter-Runtime控制器
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程
Robot Framework官方教程(一)入门
Idea hidden Idea folder hides.Iml files
MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
Jmeter-If控制器
Linux redis download and installation
pip更新命令
Lanqiao cup provincial training camp - commonly used STL
Blue Bridge Cup - binary conversion exercise