Skip to content

Commit ca75a75

Browse files
authored
add reverse tree (#89)
1 parent 2c123b6 commit ca75a75

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
2626
* [Equal Sum Sub-arrays](./array/equal_sum_subarrays_test.go)
2727
* [Rotate K Times](./array/rotate_k_steps_test.go)
2828
* [Bubble Sort](./array/bubble_sort_test.go)
29-
* [Insertion Sort](./array/insertion_sort_test.go), [Solution](./array/insertion_sort.go)
29+
* [Insertion Sort](./array/insertion_sort_test.go)
3030
* [Strings](./strings/README.md)
3131
* [The longest Dictionary Word Containing Key](./strings/longest_dictionary_word_test.go)
3232
* [Look and Tell](./strings/look_and_tell_test.go)
@@ -38,7 +38,7 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
3838
* [Linked List Serialization](./linkedlist/serialization_test.go)
3939
* [Reverse a Linked List In-place](./linkedlist/reverse_in_place_test.go)
4040
* [Join Two Sorted Linked Lists](./linkedlist/join_sorted_lists_test.go)
41-
* [Keep Repetitions](./linkedlist/keep_repetitions_test.go), [Solution](./linkedlist/keep_repetitions_test.go)
41+
* [Keep Repetitions](./linkedlist/keep_repetitions_test.go)
4242
* [Copy Linked List with Random Pointer](./linkedlist/copy_linklist_with_random_pointer_test.go)
4343
* [Implement LRU Cache](./linkedlist/lru_cache_test.go)
4444
* [Stacks](./stack/README.md)
@@ -65,14 +65,15 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
6565
* [Evaluate A Binary Expression Tree](./tree/evaluate_expression_test.go)
6666
* [Sorted Array to Balanced BST](./tree/sorted_array_to_balanced_bsd_test.go)
6767
* [Traverse Binary Tree](./tree/traverse_binary_tree_test.go)
68+
* [Reverse Binary Tree](./tree/reverse_binary_tree_test.go)
6869
* [Implement Autocomplete](./tree/auto_complete_test.go)
6970
* [Heaps](./heap/README.md)
7071
* [Kth Largest Element](./heap/kth_largest_element_test.go)
7172
* [Merge Sorted Lists](./heap/merge_sorted_list_test.go)
7273
* [Median in a Stream](./heap/median_in_a_stream_test.go)
7374
* [Kth Closest Points to the Center](./heap/k_closest_points_to_origin_test.go)
7475
* [Sliding Maximum](./heap/sliding_maximum_test.go)
75-
* [Heap Sort](./heap/heap_sort_test.go), [Solution](./heap/heap_sort.go)
76+
* [Heap Sort](./heap/heap_sort_test.go)
7677
* Algorithms
7778
* [Recursion](./recursion/README.md)
7879
* [Reverse an integer recursively](./recursion/reverse_number_test.go)

heap/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ Priority queues implemented as heaps are utilized in job scheduling, for example
9595

9696
## Rehearsal
9797

98-
* [Kth Largest Element](kth_largest_element_test.go), [Solution](kth_largest_element.go)
99-
* [Merge Sorted Lists](merge_sorted_list_test.go), [Solution](merge_sorted_list.go)
100-
* [Median in a Stream](median_in_a_stream_test.go), [Solution](median_in_a_stream_test.go)
101-
* [Kth Closest Points to the Center](k_closest_points_to_origin_test.go), [Solution](k_closest_points_to_origin.go)
102-
* [Sliding Maximum](sliding_maximum_test.go), [Solution](sliding_maximum.go)
103-
* [Heap Sort](heap_sort_test.go), [Solution](heap_search.go)
98+
* [Kth Largest Element](./kth_largest_element_test.go), [Solution](./kth_largest_element.go)
99+
* [Merge Sorted Lists](./merge_sorted_list_test.go), [Solution](./merge_sorted_list.go)
100+
* [Median in a Stream](./median_in_a_stream_test.go), [Solution](./median_in_a_stream_test.go)
101+
* [Kth Closest Points to the Center](./k_closest_points_to_origin_test.go), [Solution](./k_closest_points_to_origin.go)
102+
* [Sliding Maximum](./sliding_maximum_test.go), [Solution](./sliding_maximum.go)
103+
* [Heap Sort](./heap_sort_test.go), [Solution](./heap_sort.go)

tree/README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ Trees, such as Binary Search Trees (BSTs), can offer a time complexity of O(Log
7070

7171
## Rehearsal
7272

73-
* [Serialize Binary Tree](serialize_tree_test.go), [Solution](serialize_tree.go)
74-
* [Evaluate A Binary Expression Tree](evaluate_expression_test.go), [Solution](evaluate_expression.go)
75-
* [Sorted Array to Balanced BST](sorted_array_to_balanced_bsd_test.go), [Solution](sorted_array_to_balanced_bsd.go)
76-
* [Traverse Binary Tree](traverse_binary_tree_test.go), [Solution](traverse_binary_tree.go)
77-
* [Implement Autocomplete](auto_complete_test.go), [Solution](auto_complete.go)
73+
* [Serialize Binary Tree](./serialize_tree_test.go), [Solution](./serialize_tree.go)
74+
* [Evaluate A Binary Expression Tree](./evaluate_expression_test.go), [Solution](./evaluate_expression.go)
75+
* [Sorted Array to Balanced BST](./sorted_array_to_balanced_bsd_test.go), [Solution](./sorted_array_to_balanced_bsd.go)
76+
* [Traverse Binary Tree](./traverse_binary_tree_test.go), [Solution](./traverse_binary_tree.go)
77+
* [Reverse Binary Tree](./reverse_binary_tree_test.go), [Solution](./reverse_binary_tree_test.go)
78+
* [Implement Autocomplete](./auto_complete_test.go), [Solution](./auto_complete.go)

tree/reverse_binary_tree.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tree
2+
3+
// ReverseBinaryTree solves the problem in O(n) time and O(1) space.
4+
func ReverseBinaryTree(root *BinaryTreeNode) *BinaryTreeNode {
5+
if root == nil {
6+
return nil
7+
}
8+
root.Left, root.Right = ReverseBinaryTree(root.Right), ReverseBinaryTree(root.Left)
9+
return root
10+
}

tree/reverse_binary_tree_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tree
2+
3+
import "testing"
4+
5+
/*
6+
TestReverseBinaryTree tests solution(s) with the following signature and problem description:
7+
8+
func ReverseBinaryTree(root *BinaryTreeNode) *BinaryTreeNode
9+
10+
11+
1
12+
/ \
13+
2 3
14+
/ \ / \
15+
4 5 6 7
16+
17+
Reverse a binary search tree, the above tree should become:
18+
19+
1
20+
/ \
21+
3 2
22+
/ \ / \
23+
7 6 5 4
24+
*/
25+
func TestReverseBinaryTree(t *testing.T) {
26+
tests := []struct {
27+
tree, reversed string
28+
}{
29+
{"", ""},
30+
{"1,2,3", "1,3,2"},
31+
{"1,2,nil", "1,nil,2"},
32+
{"1,2,3,4,5,6,7", "1,3,2,7,6,5,4"},
33+
{"1,2,3,4", "1,3,2,nil,nil,nil,4"},
34+
}
35+
36+
for i, test := range tests {
37+
if got := Serialize(ReverseBinaryTree(Unserialize(test.tree))); got != test.reversed {
38+
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.reversed, got)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)