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

哈希表+树单节点(leetcode.690)

哈希表+树单节点(leetcode.690)

员工重要性

思路:

其实就是遍历树找到,以某个节点为根节点的子树,然后便利这颗子树,树中元素存储的都是一个结构体,包含id、价值、和子树节点,因为子树中的子树节点给出,所以使用哈希表存储给定数组中的根节点即可,然后根据子树节点找到对应的子树,构建的过程中在每个子树中取出其价值累加就可以

code:

/*
// Definition for Employee.
class Employee {
public:int id;int importance;vector<int> subordinates;
};
*/class Solution {
public:int getImportance(vector<Employee*> employees, int id) {unordered_map<int, Employee*> _map;auto dfs = \[&](auto&& dfs, int emp_id) -> int{Employee* e = _map[emp_id];int totel = e->importance;for(int subId : e->subordinates){totel += dfs(dfs, subId);}return totel;};for(auto employee : employees){_map[employee->id] = employee;}return dfs(dfs, id);}
};

http://www.mrgr.cn/news/11309.html

相关文章:

  • java 自定义线程池
  • Go 安全使用goroutine
  • 浪潮服务器主板集成RAID常见问题
  • C++核心编程02——引用
  • 分享一个基于Python的广东热门旅游数据可视化分析系统flask毕设(源码、调试、LW、开题、PPT)
  • Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)
  • 基于单片机的程控电源显示控制电路设计
  • 100101-批量将指定文件夹下视频时长快进或慢放到指定时长,指定比例尺寸,例如将50S视频转为1:1尺寸的30S-UI
  • spring security怎么生成JWT返回前端,以及怎么自定义JWT认证过滤器
  • Qt WebSocket
  • Vue3学习——Node环境安装(一)
  • 扫描件转word如何操作?分享3个转换技巧,简单高效
  • OpenLDN
  • GIMP简单应用: 将图片输出为指定分辨率
  • 浅谈【数据结构】图-图的概念
  • LVS部署——DR集群
  • C++中常见的数据结构
  • Physics of Language Models学习小结
  • Java中等题-整数拆分(力扣)
  • 趣味算法------猴子吃桃(循环,递归双重解法)