1. 程式人生 > 其它 >[LeetCode] 2049. Count Nodes With the Highest Score

[LeetCode] 2049. Count Nodes With the Highest Score

There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, where parents[i] is the parent of node i. Since node 0 is the root, parents[0] == -1.

Each node has a score. To find the score of a node, consider if the node and the edges connected to it were removed. The tree would become one or more non-empty subtrees. The size of a subtree is the number of the nodes in it. The score of the node is the product of the sizes of all those subtrees.

Return the number of nodes that have the highest score.

Example 1:

Input: parents = [-1,2,0,2,0]
Output: 3
Explanation:
- The score of node 0 is: 3 * 1 = 3
- The score of node 1 is: 4 = 4
- The score of node 2 is: 1 * 1 * 2 = 2
- The score of node 3 is: 4 = 4
- The score of node 4 is: 4 = 4
The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.

Example 2:

Input: parents = [-1,2,0]
Output: 2
Explanation:
- The score of node 0 is: 2 = 2
- The score of node 1 is: 2 = 2
- The score of node 2 is: 1 * 1 = 1
The highest score is 2, and two nodes (node 0 and node 1) have the highest score.

Constraints:

  • n == parents.length
  • 2 <= n <= 105
  • parents[0] == -1
  • 0 <= parents[i] <= n - 1 for i != 0
  • parents represents a valid binary tree.

統計最高分的節點數目。

給你一棵根節點為 0 的 二叉樹 ,它總共有 n 個節點,節點編號為 0 到 n - 1 。同時給你一個下標從 0 開始的整數陣列 parents 表示這棵樹,其中 parents[i] 是節點 i 的父節點。由於節點 0 是根,所以 parents[0] == -1 。

一個子樹的 大小 為這個子樹內節點的數目。每個節點都有一個與之關聯的 分數 。求出某個節點分數的方法是,將這個節點和與它相連的邊全部刪除 ,剩餘部分是若干個 非空 子樹,這個節點的 分數 為所有這些子樹大小的乘積 。

請你返回有 最高得分 節點的 數目 。

來源:力扣(LeetCode)
連結:https://leetcode.cn/problems/count-nodes-with-the-highest-score
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題題意不難理解,題設類似1339題,但是實現起來難度比1339題大得多,個人覺得挺值得做的,因為思路不止一種,而且比較考驗寫程式碼熟練度。這裡我提供一個思路,需要把樹的結構構造出來。

因為樹的構造需要自己做,所以我們在定義資料結構的時候,除了 node.left 和 node.right 定義樹的左孩子和右孩子,我們還可以設定一個引數 count 記錄以當前節點為根節點的子樹有多少個節點。這樣當我們計算某一個節點的分數的時候,我們就可以立馬知道這個節點的左右子樹的大小。對於一個在樹中間任意位置的節點而言,他的分數 = 整棵樹的節點數 total(記錄在根節點 root.count)- left.count - right.count - 1。

時間O(n)

空間O(n)

Java實現

 1 class Solution {
 2     long highest = 0;
 3 
 4     public int countHighestScoreNodes(int[] parents) {
 5         int n = parents.length;
 6         Node[] arr = new Node[n];
 7         // construct tree
 8         for (int i = 0; i < n; i++) {
 9             arr[i] = new Node();
10         }
11 
12         for (int i = 1; i < n; i++) {
13             // 當前節點的父節點
14             int parentId = parents[i];
15             // 如果父節點的左孩子為空,則把當前節點接到父節點的左子樹;否則就接到右子樹上
16             if (arr[parentId].left == null) {
17                 arr[parentId].left = arr[i];
18             } else {
19                 arr[parentId].right = arr[i];
20             }
21         }
22 
23         helper(arr[0]);
24 
25         for (int i = 0; i < n; i++) {
26             long product = 1;
27             int left = arr[i].left == null ? 0 : arr[i].left.count;
28             int right = arr[i].right == null ? 0 : arr[i].right.count;
29             int rest = n - 1 - left - right;
30             if (left > 0) {
31                 product *= left;
32             }
33             if (right > 0) {
34                 product *= right;
35             }
36             if (rest > 0) {
37                 product *= rest;
38             }
39             arr[i].product = product;
40             highest = Math.max(highest, product);
41         }
42 
43         int res = 0;
44         for (int i = 0; i < n; i++) {
45             if (arr[i].product == highest) {
46                 res++;
47             }
48         }
49         return res;
50     }
51 
52     // find the size of current (sub)tree
53     private int helper(Node root) {
54         if (root == null) {
55             return 0;
56         }
57         int size = helper(root.left) + helper(root.right) + 1;
58         root.count = size;
59         return size;
60     }
61 
62     class Node {
63         Node left;
64         Node right;
65         long product = 0L;
66         int count = 0;  
67         public Node() {}
68     }
69 }

相關題目

1339. Maximum Product of Splitted Binary Tree

2049. Count Nodes With the Highest Score

LeetCode 題目總結