You are given an integer array
parentof lengthnrepresenting a rooted tree with nodes labeled from 0 ton - 1.The tree is rooted at node 0, so
parent[0] = -1. For each nodeiwhere1 <= i <= n - 1,parent[i]denotes the parent of nodei.You are also given an integer array
numsof lengthn, wherenums[i]denotes the value of nodei.The weight of a node
iat depthdisnums[i] * (h - d + 1), wherehis the height of the tree.Return the sum of the weights of all nodes in the tree.
The depth of a node is the number of nodes on the path from the root to that node, inclusive, with the root having depth 1.
The height of the tree is the maximum depth among all nodes in the tree.
1 |
|