-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
187 lines (169 loc) · 3.12 KB
/
tree.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"math"
"sort"
)
type Node struct {
data int
left *Node
right *Node
hDist int
}
func height(root *Node) float64 {
if root == nil {
return 0
}
l := height(root.left)
r := height(root.right)
return math.Max(l, r) + 1.0
}
func diameter(root *Node) float64 {
if root == nil {
return 0
}
lH := height(root.left)
rH := height(root.right)
lD := diameter(root.left)
rD := diameter(root.right)
maxD := math.Max(lD, rD)
return math.Max(maxD, lH+rH+1)
}
func isLeaf(root *Node) bool {
if root == nil {
return false
}
if root.left == nil && root.right == nil {
return true
}
return false
}
func sum(root *Node) int {
if root == nil {
return 0
}
return sum(root.left) + root.data + sum(root.right)
}
func sumTree(root *Node) bool {
if root == nil {
return true
}
if isLeaf(root) {
return true
}
lsum := sum(root.left)
rsum := sum(root.right)
if (lsum+rsum == root.data) && sumTree(root.left) && sumTree(root.right) {
return true
}
return false
}
func lca(root *Node, x, y int) *Node {
if root == nil {
return nil
}
if root.data == x || root.data == y {
return root
}
l := lca(root.left, x, y)
r := lca(root.right, x, y)
if l != nil && r != nil {
return root
} else if l != nil {
return l
} else {
return r
}
}
func topView(root *Node) {
if root == nil {
return
}
hd := make(map[int]int)
var q []*Node
q = append(q, root)
for len(q) > 0 {
var n *Node
n, q = q[0], q[1:]
if _, ok := hd[n.hDist]; !ok {
hd[n.hDist] = n.data
}
//hd[n.hDist] = n.data
if n.left != nil {
n.left.hDist = n.hDist - 1
q = append(q, n.left)
}
if n.right != nil {
n.right.hDist = n.hDist + 1
q = append(q, n.right)
}
}
var keys []int
for k := range hd {
keys = append(keys, k)
}
sort.Ints(keys)
for _, v := range keys {
fmt.Printf("%d ", hd[v])
}
}
func isMirror(r1, r2 *Node) bool {
if r1 == nil || r2 == nil {
return true
}
if r1 != nil && r2 != nil {
if r1.data == r2.data {
return isMirror(r1.left, r2.left) && isMirror(r1.right, r2.right)
}
}
return false
}
func inorder(root *Node) {
if root == nil {
return
}
inorder(root.left)
fmt.Println(root.data)
inorder(root.right)
}
func inorderIter(root *Node) {
var stack []*Node
stack = append(stack, root)
for len(stack) > 0{
root = stack[len(stack)-1]
stack = stack[:len(stack)-1]
for root != nil {
stack = append(stack, root)
root = root.left
}
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
fmt.Println(top.data)
stack = append(stack, top.right)
}
}
func main() {
root := Node{26,
&Node{10,
&Node{4, nil, nil, 0},
&Node{6, nil, nil, 0}, 0},
&Node{3,
nil,
&Node{3, nil, nil, 0}, 0},
0,
}
fmt.Printf("Is sum tree: %t\n", sumTree(&root))
x := 6
y := 3
fmt.Printf("lca of %d and %d = %d\n", x, y, lca(&root, x, y).data)
fmt.Printf("top view of a tree: ")
topView(&root)
root1 := root
//root1.data = 4
fmt.Printf("\nisMirror: %t", isMirror(&root, &root1))
fmt.Printf("\nDiameter: %f", diameter(&root))
fmt.Printf("Inorder recursive: \n")
inorder(&root)
fmt.Println("")
inorderIter(&root)
}