1273. Delete Tree Nodes
https://leetcode.com/problems/delete-tree-nodes
Description
A tree rooted at node 0 is given as follows:
The number of nodes is
nodes;The value of the
i-th node isvalue[i];The parent of the
i-th node isparent[i].
Remove every subtree whose sum of values of nodes is zero.
After doing so, return the number of nodes remaining in the tree.
Example 1:
**Input:** nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
**Output:** 2Example 2:
**Input:** nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-2]
**Output:** 6Example 3:
**Input:** nodes = 5, parent = [-1,0,1,0,0], value = [-672,441,18,728,378]
**Output:** 5Example 4:
**Input:** nodes = 5, parent = [-1,0,0,1,1], value = [-686,-842,616,-739,-746]
**Output:** 5Constraints:
1 <= nodes <= 10^4parent.length == nodes0 <= parent[i] <= nodes - 1parent[0] == -1which indicates that0is the root.value.length == nodes-10^5 <= value[i] <= 10^5The given input is guaranteed to represent a valid tree.
ac
Last updated
Was this helpful?