当前位置:网站首页>力扣2049:统计最高分的节点数目
力扣2049:统计最高分的节点数目
2022-06-30 04:45:00 【轩辕龙儿】
2022年03月11日 力扣每日一题
题目
给你一棵根节点为 0 的 二叉树 ,它总共有 n 个节点,节点编号为 0 到 n - 1 。同时给你一个下标从 0 开始的整数数组 parents 表示这棵树,其中 parents[i] 是节点 i 的父节点。由于节点 0 是根,所以 parents[0] == -1 。
一个子树的 大小 为这个子树内节点的数目。每个节点都有一个与之关联的 分数 。求出某个节点分数的方法是,将这个节点和与它相连的边全部 删除 ,剩余部分是若干个 非空 子树,这个节点的 分数 为所有这些子树 大小的乘积 。
请你返回有 最高得分 节点的 数目 。
示例 1:

输入:parents = [-1,2,0,2,0] 输出:3 解释: - 节点 0 的分数为:3 * 1 = 3 - 节点 1 的分数为:4 = 4 - 节点 2 的分数为:1 * 1 * 2 = 2 - 节点 3 的分数为:4 = 4 - 节点 4 的分数为:4 = 4 最高得分为 4 ,有三个节点得分为 4 (分别是节点 1,3 和 4 )。
示例 2:

输入:parents = [-1,2,0] 输出:2 解释: - 节点 0 的分数为:2 = 2 - 节点 1 的分数为:2 = 2 - 节点 2 的分数为:1 * 1 = 1 最高分数为 2 ,有两个节点分数为 2 (分别为节点 0 和 1 )。
提示:
n == parents.length2 <= n <= 105parents[0] == -1- 对于
i != 0,有0 <= parents[i] <= n - 1 parents表示一棵二叉树。
- 树
- 深度优先搜索
- 数组
- 二叉树
个人解法
思路:
这题是要返回有 最高得分节点的 数目,那么就要将每一个节点的分数都算一遍,而每一个节点的分数,是由以下几个数的乘积,包括,该节点下左子树中节点的数目、该节点下右子树中节点的数目,以及总节点数-改节点为跟节点的树的节点数。
那么,我的解题步骤如下:
我先根据题目给的parents数组分别统计每个节点的直连子节点,将其存放进map中。
根据map运用递归求出每一个节点做为根节点的子树中的节点数,将其存入counts数组中
接下来遍历求每一个节点的分数,并且记入最大得分及节点的数量
下面是java的代码解法:
class Solution {
// 记录每一个节点作为根节点的子树中节点的数量
int[] counts;
public int countHighestScoreNodes(int[] parents) {
int size = parents.length;
// 记录每个节点的直接子节点
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < size; i++) {
map.put(i, new ArrayList<>());
}
for (int i = 1; i < size; i++) {
map.get(parents[i]).add(i);
}
// 记录每个子节点为根节点的树中节点数
counts = new int[size];
for (int i = 0; i < size; i++) {
if (counts[i] > 0) {
continue;
}
counts[i] = dfs(map.get(i), map);
}
// 遍历计算每个节点的得分并统计结果
long mul = 1;
for (int num : map.get(0)) {
mul *= counts[num];
}
int count = 1;
for (int i = 1; i < size; i++) {
long temp = 1;
for (int num : map.get(i)) {
temp *= counts[num];
}
temp *= (size - counts[i]);
if (temp > mul) {
mul = temp;
count = 1;
} else if (temp == mul) {
count++;
}
}
return count;
}
/** * 计算每个节点为根节点的树中节点数 */
private int dfs(List<Integer> list, Map<Integer, List<Integer>> map) {
if (list.size() == 0) {
return 1;
}
int count = 1;
for (int i : list) {
if (counts[i] > 0) {
count += counts[i];
} else {
count += dfs(map.get(i), map);
}
}
return count;
}
}
边栏推荐
- 為什麼win10開熱點後電腦沒有網絡?
- Approaching history, introduction to the London Guard Museum
- Encapsulating JDBC tool classes
- [control] multi agent system summary. 4. control agreement.
- QT 6.3.1conan software package release
- What is SQL injection and how to avoid it?
- Easyrecovery data recovery software recovers my photo and video data two years ago
- Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
- 史上最全的Redis基础+进阶项目实战总结笔记
- Redis implements SMS login function (II) redis implements login function
猜你喜欢

Redis implements SMS login function (I) traditional session login

Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok

Issue SSL certificate with IP address

Qos(Quality of Service)

Intern method of string

HTC vive cosmos development - handle button event

Connect to the database and run node JS running database shows that the database is missing

Autowired注解警告的解决办法

Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge

This connection is not a private connection this website may be pretending to steal your personal or financial information
随机推荐
Beanfactory creation process
Redis implements SMS login function (II) redis implements login function
Keywords implements and @override
Tea mall system based on SSM framework [project source code + database script + report]
Use of thread pool
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
Bean创建流程 与 lazy-init 延迟加载机制原理
【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter
Unreal 4 learning notes - data storage using blueprints
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
What is an optocoupler circuit and what should be paid attention to in actual use?
Implementation of one interview question one distributed lock every day
Introduction to some representations, neighbors and degrees of Graphs
Enlist soldiers and generals, draw small programs, multi-threaded display time
Singapore must visit these scenic spots during the Spring Festival
Sectigo certificate
Servlet lifecycle
史上最全的Redis基础+进阶项目实战总结笔记