The Delete Tree Nodes interview question presents a tree where each node has an associated integer value. Your task is to delete every subtree whose total sum of node values equals zero. After these deletions, you need to return the count of nodes remaining in the tree. This Delete Tree Nodes coding problem requires calculating aggregate properties from the bottom up.
Microsoft uses this question to test Depth-First Search interview patterns and tree representation skills. Often, the tree is given as an array of parents rather than a standard node structure. It evaluates your ability to build an adjacency list and perform post-order calculations to determine subtree sums.
This problem follows the Post-order DFS (Bottom-Up) pattern.
sum of the subtree and the count of non-deleted nodes in that subtree.sum at a node is 0, the count returned to its parent is 0 (effectively deleting the subtree).Suppose you have a root 0 (value 1) with child 1 (value -1).
1. Its subtree sum is -1.0. Total sum = .Practice returning tuples or small objects from recursive functions. In tree problems, you often need multiple pieces of information (like sum, height, and node count) simultaneously to make a decision at the parent level.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Closest Node to Path in Tree | Hard | Solve | |
| Kill Process | Medium | Solve | |
| Employee Importance | Medium | Solve | |
| Most Profitable Path in a Tree | Medium | Solve | |
| Height of Binary Tree After Subtree Removal Queries | Hard | Solve |