Given a binary tree and value k. delete all the leaf nodes with value equal to k. If a node becomes leaf after deletion, then it should be deleted if it has value k.
Input : 4 / \ 5 5 / \ / 3 1 5 Output : 4 / 5 / \ 3 1
1. Use PostOrder traversal.
2. When we encounter leaf nodes, then we check whether it is leaf node or not.
3. If it is leaf node and value equal to k, then delete it.
4. Else, Recurse for other nodes.
Nodes in postorder before deletion 4 5 3 1 5 5 Nodes in post order after required deletion 4 5 3 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Delete leaf nodes with value as x
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Delete N nodes after M nodes of a linked list
- Print the longest leaf to leaf path in a Binary tree
- Delete the last leaf node in a Binary Tree
- Program to count leaf nodes in a binary tree
- Print all nodes that are at distance k from a leaf node
- Remove nodes on root to leaf paths of length < K
- Find height of a special binary tree whose leaf nodes are connected
- Iterative program to count leaf nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Sum of nodes on the longest path from root to leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Pairwise Swap leaf nodes in a binary tree
- Root to leaf path with maximum distinct nodes
- Sum of leaf nodes at minimum level
- Count Non-Leaf nodes in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Maximum number of leaf nodes that can be visited within the given budget
- Product of all leaf nodes of binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.