当前位置:网站首页>Li Kou 2049: count the number of nodes with the highest score
Li Kou 2049: count the number of nodes with the highest score
2022-06-30 04:50:00 【Xuanyuan longer】
2022 year 03 month 11 Japan Try to make a daily question
subject
Give you a tree with a root node of 0 Of Binary tree , It has a total of n Nodes , Node number is 0 To n - 1 . And give you a subscript from 0 The starting array of integers parents This tree , among parents[i] Is the node i Parent node . Due to the node 0 It's a root , therefore parents[0] == -1 .
Of a subtree size For the number of nodes in this subtree . Each node has an associated fraction . The way to find the score of a node is , All the nodes and the edges connected to it Delete , The rest are several Non empty subtree , Of this node fraction For all these subtrees The product of size .
Please return there The highest score Node number .
Example 1:

Input :parents = [-1,2,0,2,0] Output :3 explain : - node 0 The score of is :3 * 1 = 3 - node 1 The score of is :4 = 4 - node 2 The score of is :1 * 1 * 2 = 2 - node 3 The score of is :4 = 4 - node 4 The score of is :4 = 4 The highest score is 4 , There are three nodes with scores of 4 ( They are nodes 1,3 and 4 ).
Example 2:

Input :parents = [-1,2,0] Output :2 explain : - node 0 The score of is :2 = 2 - node 1 The score of is :2 = 2 - node 2 The score of is :1 * 1 = 1 The highest score is 2 , There are two nodes with a score of 2 ( They are nodes 0 and 1 ).
Tips :
n == parents.length2 <= n <= 105parents[0] == -1- about
i != 0, Yes0 <= parents[i] <= n - 1 parentsRepresents a binary tree .
- Trees
- Depth-first search
- Array
- Binary tree
Personal solution
Ideas :
This question is to return to The highest score Node number , Then you have to calculate the score of each node , And the score of each node , Is the product of the following numbers , Include , The number of nodes in the left subtree under this node 、 The number of nodes in the right subtree under this node , And the total number of nodes - Change the node to the number of nodes in the tree following the node .
that , My problem solving steps are as follows :
I gave it first according to the topic parents The array counts the direct child nodes of each node , Store it in map in .
according to map Use recursion to find the number of nodes in the subtree with each node as the root node , Deposit it in counts Array
Next, traverse to find the score of each node , And record the maximum score and the number of nodes
Here is java Code solution of :
class Solution {
// Record the number of nodes in the subtree of each node as the root node
int[] counts;
public int countHighestScoreNodes(int[] parents) {
int size = parents.length;
// Record the direct child nodes of each node
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);
}
// Record the number of nodes in the tree where each child node is the root node
counts = new int[size];
for (int i = 0; i < size; i++) {
if (counts[i] > 0) {
continue;
}
counts[i] = dfs(map.get(i), map);
}
// Traverse and calculate the score of each node and count the results
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;
}
/** * Calculate the number of nodes in the tree where each node is the root node */
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;
}
}
边栏推荐
- Static keyword
- Arrays class
- 力扣589:N 叉树的前序遍历
- MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
- 史上最全的Redis基础+进阶项目实战总结笔记
- Yolov5 torch installation
- Efficiency test of adding and querying ArrayList and LinkedList
- File system and directory operations
- Unity realizes rotation and Revolution
- Method of applying for code signing certificate by enterprise
猜你喜欢

Malignant bug: 1252 of unit MySQL export

Cheap SSL certificate abroad

Redis implements SMS login function (II) redis implements login function

Geotrustov wildcard

Have a heart beating Valentine's day in Singapore

【Paper】2017_ Distributed control for high-speed trains movements

Process architecture and process management
![[UGV] schematic diagram of UGV version 32](/img/4b/03471d2cc96be5d57c97fd1c4e17dc.jpg)
[UGV] schematic diagram of UGV version 32

史上最全的Redis基础+进阶项目实战总结笔记

Lambda&Stream
随机推荐
Circle center technology, very anxious?
A virtual reality secret room escape adventure, let you see Technology Singapore
Transport layer protocol tcp/udp
The difference between get and post requests
Check London attractions suitable for parents and children in winter vacation
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
Qos(Quality of Service)
Thread safety and processing caused by multithreading
為什麼win10開熱點後電腦沒有網絡?
C # Foundation
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
Recommended cultural landmarks of these tourist attractions in Bangkok
圆心科技,很焦虑?
Beanfactory creation process
Encapsulating JDBC tool classes
一条命令运行rancher
Network high concurrency
Redis实现短信登入功能(二)Redis实现登入功能
Create a simple battle game with photon pun
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator