当前位置:网站首页>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;
}
}
边栏推荐
- MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
- Implementation of one interview question one distributed lock every day
- Unity a* road finding force planning
- Autowired注解警告的解决办法
- 深度学习------不同方法实现Inception-10
- 0 foundation starts self-study unit notes control direction becomes larger
- Redis实现短信登入功能(一)传统的Session登入
- Foreign SSL certificate
- One interview question a day the difference between B tree and b+ tree
- Check London attractions suitable for parents and children in winter vacation
猜你喜欢

PBR material: basic principle and simple fabrication

【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter

Deeply understand the function calling process of C language
![[UAV] kinematic analysis from single propeller to four rotor UAV](/img/32/1a88b102f832ffbbc1a7e57798260a.jpg)
[UAV] kinematic analysis from single propeller to four rotor UAV

System programming summary

Process architecture and process management

Singapore must visit these scenic spots during the Spring Festival

【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai

Introduction to some representations, neighbors and degrees of Graphs

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc
随机推荐
Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
Oculus quest2 development: (I) basic environment construction and guide package
Unity script life cycle and execution sequence
2021-03-16
National Museum of Singapore - give you spiritual and physical satisfaction
Webots learning notes
BeanFactory创建流程
Unity3d lookat parameter description
Efficiency test of adding and querying ArrayList and LinkedList
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
[FPGA] IIC读写EEPROM 的实现
Unity enables simple music visualization
System programming summary
Process architecture and process management
UE4 method of embedding web pages
Some books you should not miss when you are new to the workplace
Cheap SSL certificate abroad
PS1 Contemporary Art Center, Museum of modern art, New York
File and IO
The role of break