当前位置:网站首页>The 17th day of the special assault version of the sword offer
The 17th day of the special assault version of the sword offer
2022-08-02 09:21:00 【hys__handsome】
容易想到 O ( N 2 ) O(N^2) O(N2)brute force traversal algorithm,本质上跟forDouble-loop brute force traversal is similar,That is, the skills required to implement the code are more and more novel.
class Solution {
public:
//保证rootEach node in each of the following paths is picked,There will be no unselected nodes in the middle of the path
int dfs(TreeNode *root, int targetSum) {
if(!root) return 0;
int cnt = 0;
if(root->val == targetSum) cnt++;
cnt += dfs(root->left, targetSum - root->val);
cnt += dfs(root->right, targetSum - root->val);
return cnt;
}
int pathSum(TreeNode* root, int targetSum) {
if(!root) return 0;
//选取root点
int ans = dfs(root,targetSum);
//不选root点
ans += pathSum(root->left, targetSum);
ans += pathSum(root->right, targetSum);
return ans;
}
};
仔细思考会发现,There is a lot of double counting here,You can use prefix sums to optimize into O ( N ) O(N) O(N) 时间复杂度,where the prefix and value are placedmap的键上,The value is the number,This eliminates the need to re-traverse the previous prefix sum,Strictly speaking, because it is usedmapThis results in an algorithmic complexity of O ( N ∗ l o g N ) O(N*logN) O(N∗logN).
class Solution {
private:
unordered_map<long long, int> prefix;
// int ans = 0;
public:
int dfs(TreeNode *root, long long sum, int targetSum) {
if(!root) return 0;
sum += root->val;
int ans = 0;
if(prefix.count(sum-targetSum)) {
//sum - x = targetSum,其中xIt is a prefix sum in front of it
ans += prefix[sum-targetSum];
}
prefix[sum]++;
ans += dfs(root->left, sum, targetSum);
ans += dfs(root->right, sum, targetSum);
prefix[sum]--; //回溯,Otherwise, the sum of the nodes of other branches will be used
return ans;
}
int pathSum(TreeNode* root, int targetSum) {
prefix[0] = 1; //什么都没选,when arootThe sum of the starting path nodes is equal to targetSum时会用到
return dfs(root, 0, targetSum);
}
};
Use post-order traversal+DP思想
- Post-order traversal is to first find the maximum value of the left and right subpaths
- DPThe idea is that the current stack frame will calculate the current node+Left subpath+Right subpath(This cannot be backtracked to the parent node,Because the path is not linear),No matter what you want to backtrack to the parent node to the current node、当前+Left subpath、当前+Right subpath,The final result is the global maximum path sum.
class Solution {
private:
int res = INT_MIN;
public:
int maxPathSum(TreeNode* root) {
maxGain(root);
return res;
}
int maxGain(TreeNode *root) {
if(root == nullptr) return 0;
int left = maxGain(root->left);
int right = maxGain(root->right);
res = max(res, root->val); //Consider both the left and right paths to be negative
res = max(res, root->val + left);
res = max(res, root->val + right);
res = max(res, root->val + left + right);
//Backtracking can only return the current node value 或 当前+左 或 当前+右
return max(max(root->val,root->val+left), root->val+right);
}
};
代码简化版,It is better not to select when left and right are negative values,就为0
class Solution {
private:
int res = INT_MIN;
public:
int maxPathSum(TreeNode* root) {
maxGain(root);
return res;
}
int maxGain(TreeNode *root) {
if(root == nullptr) return 0;
//It is better not to select when left and right are negative values,就为0
int left = max(0,maxGain(root->left));
int right = max(0,maxGain(root->right));
res = max(res, root->val + left + right);
//Backtracking can only return the current node value 或 当前+左 或 当前+右
return max(left,right) + root->val;
}
};
最容易想到的方法,Save the in-order traversal sequence first,Then specify the right node of each node in sequence order,This is equivalent to creating a linked list
class Solution {
public:
void inorder(vector<TreeNode*> &ls, TreeNode *root) {
if(!root) return;
inorder(ls,root->left);
ls.emplace_back(root);
inorder(ls,root->right);
}
TreeNode* increasingBST(TreeNode* root) {
vector<TreeNode*> ls;
inorder(ls,root);
auto head = new TreeNode(-1);
auto tmp = head;
for(auto cur: ls) {
cur->left = cur->right = nullptr;
tmp->right = cur;
tmp = cur;
}
return head->right;
}
};
Come up with a harder way,Change the direction of the node pointer while traversing.Grab the inorder traversal algorithm->Traversal order invariant features.Then follow the traversal order with a move pointer.(有个坑,Move pointers cannot be placed in function parameters,A global variable is required,Because of the recursive backtracking process,Moving the pointer is still the same as it was and didn't change)
class Solution {
private:
TreeNode *cur = nullptr;
public:
void inorder(TreeNode *root) {
if(!root) return;
inorder(root->left);
root->left = nullptr;
cur->right = root;
cur = cur->right;
inorder(root->right);
}
TreeNode* increasingBST(TreeNode* root) {
auto head = new TreeNode(-1);
cur = head;
inorder(root);
return head->right;
}
};
边栏推荐
- OneinStack多版本PHP共存
- uvm-phase机制
- HCIP笔记第十三天
- Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
- mysql连接池的实现
- 【SeaTunnel】从一个数据集成组件演化成企业级的服务
- 瑞吉外卖项目剩余功能补充
- 在全志V853开发板试编译QT测试
- Redis数据结构
- It's time for bank data people who are driven crazy by reporting requirements to give up using Excel for reporting
猜你喜欢
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
使用scrapy 把爬到的数据保存到mysql 防止重复
单词接龙 II
新起点丨MeterSphere开源持续测试平台v2.0发布
PyCharm usage tutorial (more detailed, picture + text)
spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
leetcode:81. 搜索旋转排序数组 II
nacos项目搭建
uvm-phase机制
随机推荐
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
Nodejs3day(express简介,express创建基本Web服务器,托管静态资源,nodemon下载及出现的问题,中间件,编写GET,POST,JSONP接口)
[Must read] Mylander valuation analysis, electrical stimulation products for pelvic and postpartum rehabilitation
leetcode 62. Unique Paths(独特的路径)
CFdiv2-The Number of Imposters-(两种点集图上染色问题总结)
测试时大量TIME_WAIT
大厂外包,值得拥有吗?
【并发编程】- 线程池使用DiscardOldestPolicy策略、DiscardPolicy策略
软件exe图标变记事本或浏览器、360压缩打不开的几种应急解决方法
瑞吉外卖项目剩余功能补充
The use of thread pool and analysis of ThreadPoolExecutor source code
Golang ORM框架 — GORM
Postman download localization of installation and use
在全志V853开发板试编译QT测试
Jenkins--基础--07--Blue Ocean
nacos项目搭建
练习40,小蓝的旅行【最短路】
剑指offer专项突击版第17天
PyQt5安装配置(PyCharm) 亲测可用
PyCharm usage tutorial (more detailed, picture + text)