Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Delete Leaves With a Given Value

Problem Statement

Given a root node of a binary tree and an integer target, delete all leaf nodes that have a value equal to target.

After removing such leaf nodes, if any of their parent nodes become leaf nodes with a value equal to target, they should also be removed. This process should continue until no more nodes can be deleted.

Examples

Example 1

  • Input: root = [1, 5, 3, 5, null, 5, 4], Target: 5
  • Expected Output: [1,null,3,null,4]
    • Explanation: The left child of 5 (left of root) is 3 and 5

.....

.....

.....

Like the course? Get enrolled and start learning!