Skip to content

Commit bc23627

Browse files
committed
reverse the values of the binary tree from the alternate levels
1 parent cddad74 commit bc23627

File tree

3 files changed

+138
-5
lines changed

3 files changed

+138
-5
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# GO - Data Structures and Algorithms
22

3-
Inspired by the **[Geeksforgeeks - Top 10 Algorithms in Interview Questions](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/)** article, the intent of this repository is to solve these questions using the **Go Language**. GO is a great language choice for technical interviews and hopefully you can find the solutions to the common algorithms/problems easy to understand.
3+
Inspired by the **[Geeksforgeeks - Top 10 Algorithms in Interview Questions](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/)** article, the intent of this repository is to solve these questions using the **Go Language**. GO is a great language choice for technical interviews and hopefully you can find these solutions to the common algorithms/problems easy to understand.
44

5-
Although this is an introductory course to algorithms and data structures, it assumes that you are familiar with GO programming language syntax and basic concepts.
5+
Although this is an introductory material to algorithms and data structures, it assumes that you are familiar with GO programming language syntax and basic concepts.
66

77
WIP, the descriptions of the below `unsolved yet` problems can be found in the [orginal article](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/).
88

9-
> ***Contributions are welcomed - submit a PR***.
9+
> ***Contributions are welcomed - solve a problem and submit a PR***.
1010
> ***Contribution guidelines***
1111
> * keep the consistency and document the code
12-
> * keep it simple, easy to read and understand (not over-engineered, best performance is not in scope)
12+
> * optimize for readability and simplicity (not over-engineered, best performance is not in scope)
1313
1414
## [Graph](https://github.com/danrusei/algorithms_with_Go/tree/main/graph)
1515

@@ -74,7 +74,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
7474
- [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
77-
- [] Reverse alternate levels of a perfect binary tree
77+
- [x] [Reverse alternate levels of a perfect binary tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/reverse_alternate)
7878

7979
## [Number Theory](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers)
8080

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Reverse alternate levels of a perfect binary tree
2+
3+
Problem description: [GeeksforGeeks](https://www.geeksforgeeks.org/reverse-alternate-levels-binary-tree/amp/)
4+
5+
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.
6+
7+
## Example
8+
9+
```bash
10+
Given tree:
11+
11
12+
/ \
13+
12 13
14+
/ \ / \
15+
14 15 16 17
16+
/ \ / \ / \ / \
17+
1 2 3 4 5 6 7 8
18+
19+
Modified tree:
20+
11
21+
/ \
22+
13 12
23+
/ \ / \
24+
14 15 16 17
25+
/ \ / \ / \ / \
26+
8 7 6 5 4 3 2 1
27+
28+
29+
```
30+
31+
## Algorithm
32+
33+
* traverse the given tree in inorder fashion and store all odd level values in a slice
34+
* reverse the slice
35+
* traverse the tree again inorder fashion, take elements from the slice and store the values to every odd level traversed node.
36+
37+
## Result
38+
39+
```bash
40+
$ go run main.go
41+
In order traversal of the original tree:
42+
((((1) 14 (2)) 12 ((3) 15 (4))) 11 (((5) 16 (6)) 13 ((7) 16 (8))))
43+
In order traversal of the modified tree:
44+
((((8) 14 (7)) 13 ((6) 15 (5))) 11 (((4) 16 (3)) 12 ((2) 16 (1))))
45+
```

binary_tree/reverse_alternate/main.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
binarytree "github.com/danrusei/algorithms_with_go/binary_tree"
7+
)
8+
9+
// traverse tree and store odd level elements
10+
func storeAlternate(tree *binarytree.Tree, store *[]int, level int) {
11+
if tree == nil {
12+
return
13+
}
14+
15+
// Inorder traverse the tree and store only the values from alternate levels
16+
storeAlternate(tree.Left, store, level+1)
17+
18+
if level%2 != 0 {
19+
*store = append(*store, tree.Value)
20+
}
21+
22+
storeAlternate(tree.Right, store, level+1)
23+
}
24+
25+
// traverse the tree and take the elements from the slice and store in odd level nodes
26+
func modifyTree(tree *binarytree.Tree, store []int, index int, level int) int {
27+
28+
if tree == nil {
29+
return index
30+
}
31+
32+
// Inorder traverse the tree and update level values from the store if the level is odd
33+
index = modifyTree(tree.Left, store, index, level+1)
34+
35+
if level%2 != 0 {
36+
tree.Value = store[index]
37+
index++
38+
}
39+
40+
index = modifyTree(tree.Right, store, index, level+1)
41+
42+
return index
43+
}
44+
45+
func reverseAlternate(tree *binarytree.Tree) {
46+
47+
//store all alternate levels nodes
48+
store := []int{}
49+
50+
// inorder traverse and store the values from alternate levels
51+
storeAlternate(tree, &store, 0)
52+
53+
// reverse the slice
54+
for i, j := 0, len(store)-1; i < j; i, j = i+1, j-1 {
55+
store[i], store[j] = store[j], store[i]
56+
}
57+
58+
index := 0
59+
_ = modifyTree(tree, store, index, 0)
60+
}
61+
62+
func main() {
63+
64+
tree := binarytree.NewTree(11)
65+
tree.Left = binarytree.NewTree(12)
66+
tree.Right = binarytree.NewTree(13)
67+
tree.Left.Left = binarytree.NewTree(14)
68+
tree.Left.Left.Left = binarytree.NewTree(1)
69+
tree.Left.Left.Right = binarytree.NewTree(2)
70+
tree.Left.Right = binarytree.NewTree(15)
71+
tree.Left.Right.Left = binarytree.NewTree(3)
72+
tree.Left.Right.Right = binarytree.NewTree(4)
73+
tree.Right.Left = binarytree.NewTree(16)
74+
tree.Right.Left.Left = binarytree.NewTree(5)
75+
tree.Right.Left.Right = binarytree.NewTree(6)
76+
tree.Right.Right = binarytree.NewTree(16)
77+
tree.Right.Right.Left = binarytree.NewTree(7)
78+
tree.Right.Right.Right = binarytree.NewTree(8)
79+
80+
fmt.Println("In order traversal of the original tree:")
81+
fmt.Println(tree)
82+
83+
reverseAlternate(tree)
84+
85+
fmt.Println("In order traversal of the modified tree:")
86+
fmt.Println(tree)
87+
88+
}

0 commit comments

Comments
 (0)