Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Delete Leaf Nodes With Value X
MEDIUM
27 mins
15 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Delete Leaf Nodes With Value X

Contributed by
Anish De
Medium
yellow-spark
0/80
Avg time to solve 27 mins
Share
15 upvotes

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, Constraints, Images )
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
Reset Code
Full screen
Auto
copy-code
Console