-
Notifications
You must be signed in to change notification settings - Fork 32
/
problem.go
46 lines (42 loc) · 907 Bytes
/
problem.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
package day50
// Operator represents the arithmetic operations.
type Operator int
const (
// NON is a zero value of this constant.
NON Operator = iota
// ADD is addition
ADD
// SUB is subtraction
SUB
// MUL is multiplication
MUL
// DIV is division
DIV
)
// ArithmeticTree represents either a node operator or a leaf value.
type ArithmeticTree struct {
val int
op Operator
left, right *ArithmeticTree
}
// Calculate takes an ArithmeticTree and returns its calculated value.
// Runs in O(N) in that it only views the tree nodes once.
func Calculate(tree *ArithmeticTree) int {
if tree.op == NON {
return tree.val
}
left := Calculate(tree.left)
right := Calculate(tree.right)
var result int
switch tree.op {
case ADD:
result = left + right
case SUB:
result = left - right
case MUL:
result = left * right
case DIV:
result = left / right
}
return result
}