Talentd
FleetCode

1273. Delete Tree Nodes

Medium61.2% acceptance1 companyPremium elsewhere, free here

A tree rooted at node 0 is given as follows:

  • The number of nodes is nodes;
  • The value of the ith node is value[i];
  • The parent of the ith node is parent[i].

Remove every subtree whose sum of values of nodes is zero.

Return the number of the remaining nodes 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: 2

Example 2:

Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-2]
Output: 6

Constraints:

  • 1 <= nodes <= 104
  • parent.length == nodes
  • 0 <= parent[i] <= nodes - 1
  • parent[0] == -1 which indicates that 0 is the root.
  • value.length == nodes
  • -105 <= value[i] <= 105
  • The given input is guaranteed to represent a valid tree.

Asked at 1 company

Hints open one at a time

Hint 1
Traverse the tree using depth first search.
Hint 2
Find for every node the sum of values of its sub-tree.
Hint 3
Traverse the tree again from the root and return once you reach a node with zero sum of values in its sub-tree.
11:46
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
7
[-1,0,0,1,2,2,2]
[1,-2,4,0,-2,-1,-1]
Input
7
[-1,0,0,1,2,2,2]
[1,-2,4,0,-2,-1,-2]
Your code is saved on this device.Solution