-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvert2.go
60 lines (46 loc) · 857 Bytes
/
invert2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
/*
* Daily Coding Problem: Problem #83
* Daily Coding Problem: Problem #596 [Medium]
*
Invert a binary tree.
For example, given the following tree:
a
/ \
b c
/ \ /
d e f
should become:
a
/ \
c b
\ / \
f e d
*/
import (
"fmt"
"os"
"binary_tree/tree"
)
func main() {
root := tree.CreateFromString(os.Args[1])
if root != nil {
fmt.Printf("digraph g1 {\n")
fmt.Printf("subgraph cluster_0 {\n\tlabel=\"before\"\n")
tree.DrawPrefixed(os.Stdout, root, "a")
fmt.Printf("\n}\n")
invert(root)
fmt.Printf("subgraph cluster_1 {\n\tlabel=\"after\"\n")
tree.DrawPrefixed(os.Stdout, root, "b")
fmt.Printf("\n}\n")
fmt.Printf("\n}\n")
}
}
func invert(node *tree.StringNode) {
if node == nil {
return
}
invert(node.Left)
invert(node.Right)
node.Left, node.Right = node.Right, node.Left
}