当前位置: 首页 > news >正文

刷题之路径总和Ⅲ(leetcode)

路径总和Ⅲ
在这里插入图片描述
这题和和《为K的数组》思路一致,也是用前缀表。
代码调试过,所以还加一部分用前序遍历数组和中序遍历数组构造二叉树的代码。

#include<vector>
#include<unordered_map>
#include<iostream>
using namespace std;
//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 {
private:unordered_map<long long, int>map;int dfs(TreeNode* root, long long cur, int targetSum){if (root == NULL){return 0;}int count = 0;cur += root->val;if (map.find(cur - targetSum) != map.end()){count += map[cur - targetSum];}map[cur]++;int leftcount = dfs(root->left, cur, targetSum);int rightcount = dfs(root->right, cur, targetSum);map[cur]--;//因为路径总和只是针对同一个头结点,所以不是同一个头结点时需要回溯return count + leftcount + rightcount;}
public:int pathSum(TreeNode* root, int targetSum) {map[0] = 1;return dfs(root, 0, targetSum);}
};class tree {
private:TreeNode* build(vector<int>& preorder, vector<int>& inorder){if (preorder.size() == 0)return NULL;//找到根节点int rootvalue = preorder[0];TreeNode* root = new TreeNode(rootvalue);//叶子节点if (preorder.size() == 1)return root;//区分左右子树位置int index = 0;for (int i = 0; i < inorder.size(); i++){if (inorder[i] == rootvalue){index = i;break;}}vector<int>left_in(inorder.begin(), inorder.begin() + index);vector<int>right_in(inorder.begin() + index + 1, inorder.end());vector<int>left_pre(preorder.begin() + 1, preorder.begin() + 1 + left_in.size());vector<int>right_pre(preorder.begin() + 1 + left_in.size(), preorder.end());root->left = build(left_pre, left_in);root->right = build(right_pre, right_in);return root;}
public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {return build(preorder, inorder);}
};int main()
{vector<int>inorder = {3,3,-2,5,2,1,10,-3,11};vector<int>preorder = { 10,5,3,3,-2,2,1,-3,11 };int targetsum = 8;tree mytree;TreeNode* root = mytree.buildTree(preorder,inorder);Solution solution;int result = solution.pathSum(root, targetsum);cout << result << endl;
}
http://www.lryc.cn/news/355649.html

相关文章:

  • MongoDB 原子操作:确保数据一致性和完整性的关键
  • 2024上半年软考高级系统架构设计师回顾
  • SQL注入绕过技术深度解析与防御策略
  • Redis教程(十六):Redis的缓存穿透、缓存击穿、缓存雪崩
  • 如何实现一个高效的单向链表逆序输出?
  • 使用 Go 实现 HelloWorld 程序,并分析其结构
  • 机器学习:在Python中sklearn库的使用,纯干货!12个小时的整理!
  • XSS 攻击
  • .Net Core 中间件与过滤器
  • 【ARMv7-A】——WFI(wait for interrupt)
  • 92. 反转链表 II
  • Modbus工业网关
  • c++——模板初始识
  • 帆软生成csv文件
  • 12.Redis之补充类型渐进式遍历
  • 品牌做电商控价的原因
  • 安全面试中的一个基础问题:你如何在数据库中存储密码?
  • 【python深度学习】——torch.min()
  • 华为校招机试 - 最久最少使用缓存(20240508)
  • 第三部分:领域驱动设计之分析模式和设计模式应用于模型
  • PID传感器在光电显示行业VOC气体检测的应用
  • iOS推送证书过期处理
  • 蓝海卓越计费管理系统 agent_setstate.php SQL注入漏洞复现
  • 【leetcode2765--最长交替子数组】
  • java文档管理系统的设计与实现源码(springboot+vue+mysql)
  • 西安航空学院电子工程学院领导莅临泰迪智能科技参观交流
  • C++|设计模式(二)|简单工厂和工厂方法模式
  • C语言从头学12——流程控制(一)
  • 10大领域应该怎么记?
  • 通过Ubuntu虚拟机+Linux移植LVGL并通过linux Frame buffer显示