Given a Binary Tree and a number k, remove all nodes that lie only on root to leaf path(s) of length smaller than k. If a node X lies on multiple root-to-leaf paths and if any of the paths has path length >= k, then X is not deleted from Binary Tree. In other words a node is deleted if all paths going through it have lengths smaller than k.
Consider the following example Binary Tree
1 / \ 2 3 / \ \ 4 5 6 / / 7 8 Input: Root of above Binary Tree k = 4 Output: The tree should be changed to following 1 / \ 2 3 / \ 4 6 / / 7 8 There are 3 paths i) 1->2->4->7 path length = 4 ii) 1->2->5 path length = 3 iii) 1->3->6->8 path length = 4 There is only one path " 1->2->5 " of length smaller than 4. The node 5 is the only node that lies only on this path, so node 5 is removed. Nodes 2 and 1 are not removed as they are parts of other paths of length 4 as well. If k is 5 or greater than 5, then whole tree is deleted. If k is 3 or less than 3, then nothing is deleted.
We strongly recommend to minimize your browser and try this yourself first
The idea here is to use post order traversal of the tree. Before removing a node we need to check that all the children of that node in the shorter path are already removed.
There are 2 cases:
i) This node becomes a leaf node in which case it needs to be deleted.
ii) This node has other child on a path with path length >= k. In that case it needs not to be deleted.
The implementation of above approach is as below :
Inorder Traversal of Original tree 7 4 2 5 1 3 8 6 Inorder Traversal of Modified tree 7 4 2 1 3 8 6
Time complexity of the above solution is O(n) where n is number of nodes in given Binary Tree.
This article is contributed by Kumar Gautam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count root to leaf paths having exactly K distinct nodes in a Binary Tree
- Print all root-to-leaf paths with maximum count of even nodes
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Sum of all the numbers that are formed from root to leaf paths
- Print root to leaf paths without using recursion
- Root to leaf paths having equal lengths in a Binary Tree
- Print all root to leaf paths with there relative positions
- Count of root to leaf paths in a Binary Tree that form an AP
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Print the path common to the two paths from the root to the two given nodes
- Sum of nodes on the longest path from root to leaf node
- Root to leaf path with maximum distinct nodes
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Print the longest leaf to leaf path in a Binary tree
- Print all the paths from root, with a specified sum in Binary tree