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 is value[i];

  • The parent of the i-th node is parent[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:

Example 2:

Example 3:

Example 4:

Constraints:

  • 1 <= nodes <= 10^4

  • parent.length == nodes

  • 0 <= parent[i] <= nodes - 1

  • parent[0] == -1 which indicates that 0 is the root.

  • value.length == nodes

  • -10^5 <= value[i] <= 10^5

  • The given input is guaranteed to represent a valid tree.

ac

Last updated

Was this helpful?