当前位置:网站首页>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);
}
};
边栏推荐
- 07【Path、Files类的使用】
- In idea, system.getproperty ("user.dir") identifies the path of the module: the setting of the working directory
- [golang] before method of time type in golang
- Read the triode easily. It turns out that it works like this
- Research on parameter setting of MATLAB FFT
- ctfshow ThinkPHP专题 1
- Selenium automated test (this one is enough) - self study
- Fiddler抓包工具总结
- 【Golang】golang实现md5加密函数
- Only "a little bit", why do developers look up to you?
猜你喜欢

Publish local images to Alibaba cloud

CSDN会员的魅力何在?我要他有什么用?

RS485 communication OSI model network layer

How to go from functional testing to automated testing?

JMeter interface test steps - Installation Tutorial - script recording - concurrent test

2022,软测人的平均薪资,看完我瞬间凉了...

Jmeter-If控制器

简单理解modbus功能码和分区

"Low power Bluetooth module" master-slave integrated Bluetooth sniffer - help smart door lock

《Nature》论文插图复刻第3期—面积图(Part2-100)
随机推荐
Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
Druid encryption command
iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?
Read the triode easily. It turns out that it works like this
Blue Bridge Cup provincial match training camp - Calculation of date
UNIX C language POSIX mutex thread synchronization
Xilinx FPGA soft core development process
聊聊软件测试-自动化测试框架
07 [use of path and files classes]
Lanqiao cup provincial training camp - stack and recursion
【Golang】golang实现md5加密函数
The solution of permission denied
[white hat talks about web security] Chapter 1 my security world view
ctfshow ThinkPHP专题 1
HDU 3351:Seinfeld
Idea background image set
自学软件测试天赋异禀——不是盖的
MySQL paging
自动推理的逻辑06--谓词演算
Fiddler packet capture tool summary