Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Delete Leaves With a Given Value

XPChallenge Points: 20
levelLevel: Medium

Given a binary tree and a number target, remove all leaf nodes that have the value equal to target.
After removing a leaf, if its parent becomes a leaf and has value target, remove it too. Continue this until no more nodes can be deleted.

Example 1:

Input: root = [5,3,6,3,null,6,7], target = 3

Output: [5,null,6,null,7]

Explanation:

Leaf nodes with value 3 are removed. Then the parent node becomes a leaf with value 3, so it is removed as well.

Example 2:

Input: root = [4,2,2,2,5], target = 2

Output: [4,2,null,null,5]

Example 3:

Input: root = [1,2,null,2,null,2], target = 2

Output: [1]

Explanation:

All leaf nodes with value 2 are removed recursively.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result