FastPrepDelete Leaves With a Target Value
Problems/Delete Leaves With a Target Value

Delete Leaves With a Target Value

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks you to delete target-valued leaves, and keep deleting as parents become leaves, using postorder traversal. It also asks how things would differ if every matching internal node were deleted. That follow-up has no rule for reconnecting children. Our version is about 95% the same as the reported question.

Given a binary tree and an integer target, repeatedly delete every leaf whose value equals target. After deletions, a parent may become a new target-valued leaf and must also be deleted.

Return the final root. Modify and reuse the surviving input nodes.

Function

removeLeafNodes(root: TreeNode, target: int) → TreeNode

Examples

Example 1

root = [1,2,3,2,null,2,4]target = 2return = [1,null,3,null,4]
root
122324
Removed
→
return
134

All target-valued leaves are removed, including the left child after its child disappears.

Example 2

root = [1,3,3,3,2]target = 3return = [1,3,null,null,2]
root
13323
Removed
→
return
132

The left 3 remains because it still has a non-target child.

Example 3

root = [1,2,null,2,null,2]target = 2return = [1]
root
1222
Removed
→
return
1

Deletion cascades upward through the chain of 2-valued nodes.

Example 4

root = []target = 1return = []
root
empty
return
empty

An empty tree remains empty.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -10^9 <= node.val, target <= 10^9.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
  • Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEW
  • Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEW
  • Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEW
  • Balanced Tree Node ReportNetflix · ONSITE INTERVIEW
  • Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEW
  • Height-Balanced Binary TreeExpedia · ONSITE INTERVIEW
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREEN
  • Validate Binary Search TreeLyft · PHONE SCREEN
  • Lowest Common Ancestor in a Binary TreeZillow · PHONE SCREEN

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
See Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode removeLeafNodes(TreeNode root, int target) {
      // Write your code here.
    }
}
Checking account…