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
- Delete leaf nodes with value as x
- Delete the last leaf node in a Binary Tree
- Delete N nodes after M nodes of a linked list
- Sum of leaf nodes at minimum level
- Print all leaf nodes of an n-ary tree using DFS
- Print Leaf Nodes at a given Level
- Sum of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Print all nodes that are at distance k from a leaf node
- Product of all leaf nodes of binary tree
- Determine the count of Leaf nodes in an N-ary tree
- Remove nodes on root to leaf paths of length < K
- Root to leaf path with maximum distinct nodes
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print Sum and Product of all Non-Leaf nodes in 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 email@example.com. 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.
Improved By : 29AjayKumar