当前位置:网站首页>High frequency leetcode deep search part: 112 Path sum
High frequency leetcode deep search part: 112 Path sum
2022-07-22 09:28:00 【Hiccup~~~~】
112. The sum of the paths
The difficulty is simple 800
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 .
Example 1:
Input :root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output :true explain : The root node to leaf node path equal to the target sum is shown in the figure above .
Example 2:
Input :root = [1,2,3], targetSum = 5 Output :false explain : There are two paths from root node to leaf node in the tree :
(1 --> 2): And for 3
(1 --> 3): And for 4
non-existent sum = 5 The path from the root node to the leaf node .
Example 3:
Input :root = [], targetSum = 0 Output :false explain : Because the tree is empty , Therefore, there is no path from root node to leaf node .
Tips :
- The number of nodes in the tree is in the range [0, 5000] Inside
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
Ideas
- Set the cumulative array , Traverse the journey down , Continue to accumulate
- Failure point : The left and right nodes are handled separately , When dealing with the right node , Its value is equivalent to adding the root and left nodes respectively ( Setup queue , The left and right nodes enter together )
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 hasPathSum(TreeNode* root, int targetSum) {
queue<TreeNode*> cur;// Nodes currently in the queue
queue<int> dp;
int flag=0;// Can't find target Satisfied path
if(root==NULL) return 0;
cur.push(root);
dp.push(root->val);
while(!cur.empty())
{
root=cur.front();
cur.pop();
int p=dp.front();
dp.pop();
if(p==targetSum){
// Determine whether the leaf node
if(root->left==NULL&&root->right==NULL){
return 1;
}
}
// Zuo Zishu joins the team
if(root->left!=NULL){
cur.push(root->left);
int a=p+root->left->val;
dp.push(a);
}
// Right subtree joins the team
if(root->right!=NULL){
cur.push(root->right);
int b=p+root->right->val;
dp.push(b);
}
}
return flag;
}
};
边栏推荐
猜你喜欢
Preliminary use of El expressions
字节流
Go死锁问题
(一)抖音快手短视频去水印原理分析
MySQL installation configuration -version 8.0 -windows
文献: Axure(简单介绍)
Luoyang comprehensive bonded zone was officially approved by the State Council to be established
排序方法:冒泡排序(使用数组实现一串数字的顺序排列(从大到小或从小到大))
各地級市-進出口與貿易差額(2000-2020)
Mvcc multi version concurrency control for MySQL learning
随机推荐
Array of C language
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
异常处理
二分法查找
Modify the hosts file to customize the local IP domain name
流
4000+上市公司所属省份、城市、经营等多指标数据已更新至2022.1
c语言之指针(四)
如何快速开发一个简单实用的MES系统?
高频leetcode深搜部分:112. 路径总和
CAN通信协议(一)
力扣题之回文数
flyio 无感刷新token
FAQ during using Harry (question answering)
Laravel installs the debugbar toolbar and configures the virtual host
Bigkey and hotspot key of redis principle
高频leetcode深搜部分:98. 验证二叉搜索树
Byte stream
SQL注入
嵌入式之网络接口方案介绍与驱动调试方式总结