Cascade Deletion of Target-Valued Leaves
Updated
Problem statement
Given a binary tree and an integer target, delete every leaf whose value equals target. Repeat this rule when deletion makes another matching node a leaf, until no matching leaf remains. Return the root of the remaining tree.
For this exercise, assume the empty tree remains empty and the root may also be deleted. Preserve the values and relative links of surviving nodes.
Function
removeTargetLeaves(root: TreeNode, target: int) → TreeNodeExamples
Example 1
root = [1,2,3,2,null,2,4]target = 2return = [1,null,3,null,4]Removed
Removing matching leaves can expose another removable matching leaf.
Example 2
root = [2,2,2]target = 2return = []The entire tree disappears when every node is removable.
Example 3
root = []target = 2return = []An empty tree remains empty.
Constraints
- For this exercise, assume a finite acyclic binary tree containing
0through500nodes. -10^6 <= node.val <= 10^6. The same bound applies totarget.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Lowest Common Ancestor in a BST and a Binary TreeGreyOrange · PHONE SCREENGreyOrange interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)