Tap here to switch tabs
Problem
Submissions

Problem: Weighted Sum of a Tree

Try to solve the Weighted Sum of a Tree problem.

Statement

You are given an integer array parent representing a rooted tree with nn nodes numbered from 00 to n−1n - 1, where parent[i] is the parent of node i. The root of the tree is node 00, so parent[0] == −1-1. You are also given an integer array nums of length nn, where nums[i] represents the value assigned to node i.

The depth of a node is the number of nodes on the path from the root node 00 to that node. Thus, the root node 00 has a depth of 11.

Let HH be the maximum depth among all nodes in the tree (the height of the tree). The weight of node i with depth did_i is calculated as:

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Weighted Sum of a Tree

Try to solve the Weighted Sum of a Tree problem.

Statement

You are given an integer array parent representing a rooted tree with nn nodes numbered from 00 to n−1n - 1, where parent[i] is the parent of node i. The root of the tree is node 00, so parent[0] == −1-1. You are also given an integer array nums of length nn, where nums[i] represents the value assigned to node i.

The depth of a node is the number of nodes on the path from the root node 00 to that node. Thus, the root node 00 has a depth of 11.

Let HH be the maximum depth among all nodes in the tree (the height of the tree). The weight of node i with depth did_i is calculated as: