Tap here to switch tabs
Problem
Submissions

Problem: Delete Tree Nodes

Try to solve the Delete Tree Nodes problem.

Statement

Given a tree with nodes nodes labeled from 00 to nodes −1- 1, rooted at node 00. You are given an integer array parent of size nodes, where parent[i] represents the parent of node i, and an integer array value of size nodes, where value[i] denotes the value associated with node i.

If the sum of values of all nodes in a subtree is equal to 00, the entire subtree rooted at that node is removed.

Return the total number of nodes remaining in the tree after deleting all subtrees with a sum of 00.

Constraints:

  • 1≤1 \leq nodes ≤104\leq 10^4

  • parent.length ==== nodes

  • 0≤0 \leq parent[i] ≤\leq nodes −1- 1

  • parent[0] ==−1== -1, which indicates that 00 is the root

  • value.length ==== nodes

  • −105≤-10^5 \leq value[i] ≤105\leq 10^5

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

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Delete Tree Nodes

Try to solve the Delete Tree Nodes problem.

Statement

Given a tree with nodes nodes labeled from 00 to nodes −1- 1, rooted at node 00. You are given an integer array parent of size nodes, where parent[i] represents the parent of node i, and an integer array value of size nodes, where value[i] denotes the value associated with node i.

If the sum of values of all nodes in a subtree is equal to 00, the entire subtree rooted at that node is removed.

Return the total number of nodes remaining in the tree after deleting all subtrees with a sum of 00.

Constraints:

  • 1≤1 \leq nodes ≤104\leq 10^4

  • parent.length ==== nodes

  • 0≤0 \leq parent[i] ≤\leq nodes −1- 1

  • parent[0] ==−1== -1, which indicates that 00 is the root

  • value.length ==== nodes

  • −105≤-10^5 \leq value[i] ≤105\leq 10^5

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