Update appNew update is available. Click here to update.

Delete Leaf Nodes With Value X

profile
Contributed by
Anish De
Medium
yellow-spark
0/80
27 mins
17 upvotes
MakeMyTripSprinklrGoogle
+2 more

Problem Statement

You are given a binary tree, in which the data present in the nodes are integers. You are also given an integer X.

Your task is to delete all the leaf nodes with value X. In the process, if the newly formed leaves also have value X, you have to delete them too.

For example:

For the given binary tree, and X = 3:

tree

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 100
0 <= N <= 3*10^3
1 <= A <= 10^5
1 <= X <= 10^5

Time limit: 1sec
Sample Input 1:
1
1 3 3 2 3 -1 3 -1 -1 -1 -1 -1 -1
3
Sample Output 1:
2 3 1
Explanation For Sample Input 1:

tree

Sample Input 2:
1
1 2 2 3 3 3 3 -1 -1 -1 -1 -1 -1 -1 -1
3
Sample Output 2:
2 1 2
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console