Skip to content

Commit cddad74

Browse files
committed
remove nodes from a binary treei based on length
1 parent d3d0246 commit cddad74

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
7171
- [] Check whether a binary tree is a full binary tree or not
7272
- [x] [Bottom View Binary Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/bottom_view)
7373
- [] Print Nodes in Top View of Binary Tree
74-
- [] Remove nodes on root to leaf paths of length < K
74+
- [x] [Remove nodes on root to leaf paths of length < K](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/remove_nodes)
7575
- [] Lowest Common Ancestor in a Binary Search Tree
7676
- [] Check if a binary tree is subtree of another binary tree
7777
- [] Reverse alternate levels of a perfect binary tree

binary_tree/remove_nodes/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Remove nodes on root to leaf paths of length < K
2+
3+
Problem description: [GeeksforGeeks](https://www.geeksforgeeks.org/remove-nodes-root-leaf-paths-length-k/amp/)
4+
5+
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.
6+
7+
## Example
8+
9+
Consider the following example Binary Tree
10+
11+
```bash
12+
1
13+
/ \
14+
2 3
15+
/ \ \
16+
4 5 6
17+
/ /
18+
7 8
19+
20+
Input: Root of above Binary Tree
21+
k = 4
22+
23+
Output: The tree should be changed to following
24+
1
25+
/ \
26+
2 3
27+
/ \
28+
4 6
29+
/ /
30+
7 8
31+
32+
There are 3 paths
33+
i) 1->2->4->7 path length = 4
34+
ii) 1->2->5 path length = 3
35+
iii) 1->3->6->8 path length = 4
36+
37+
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.
38+
39+
If k is 5 or greater than 5, then whole tree is deleted.
40+
If k is 3 or less than 3, then nothing is deleted.
41+
```
42+
43+
## Algorithm
44+
45+
* Traverse the tree in postorder fashion so that if a leaf node path length is shorter than k, then that node and all of its descendants till the node which are not on some other path are removed.
46+
* If root is a leaf node and it's level is less than k then remove this node.
47+
48+
## Result
49+
50+
```bash
51+
$ go run main.go
52+
In order traversal of the original tree:
53+
((((7) 4) 2 (5)) 1 (3 ((8) 6)))
54+
In order traversal of the modified tree:
55+
((((7) 4) 2) 1 (3 ((8) 6)))
56+
```

binary_tree/remove_nodes/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
binarytree "github.com/danrusei/algorithms_with_go/binary_tree"
7+
)
8+
9+
func removeShortPath(tree *binarytree.Tree, level int, k int) *binarytree.Tree {
10+
if tree == nil {
11+
return nil
12+
}
13+
14+
// Traverse the tree in postorder fashion so that if a leaf
15+
// node path length is shorter than k, then that node and
16+
// all of its descendants till the node which are not
17+
// on some other path are removed.
18+
tree.Left = removeShortPath(tree.Left, level+1, k)
19+
tree.Right = removeShortPath(tree.Right, level+1, k)
20+
21+
if tree.Left == nil && tree.Right == nil && level < k {
22+
tree = nil
23+
}
24+
25+
return tree
26+
}
27+
28+
func main() {
29+
30+
tree := binarytree.NewTree(1)
31+
tree.Left = binarytree.NewTree(2)
32+
tree.Right = binarytree.NewTree(3)
33+
tree.Left.Left = binarytree.NewTree(4)
34+
tree.Left.Right = binarytree.NewTree(5)
35+
tree.Left.Left.Left = binarytree.NewTree(7)
36+
tree.Right.Right = binarytree.NewTree(6)
37+
tree.Right.Right.Left = binarytree.NewTree(8)
38+
39+
fmt.Println("In order traversal of the original tree:")
40+
fmt.Println(tree)
41+
42+
// this is the distance of the shortest path
43+
k := 4
44+
45+
modifiedTree := removeShortPath(tree, 1, k)
46+
47+
fmt.Println("In order traversal of the modified tree:")
48+
fmt.Println(modifiedTree)
49+
50+
}

0 commit comments

Comments
 (0)