Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 50
Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'.

Given the root to such a tree, write a function to evaluate it.

For example, given the following tree:
```
    *
   / \
  +    +
 / \  / \
3  2  4  5
```
You should return 45, as it is (3 + 2) * (4 + 5).

Pull request:
Day50
Resolves #110 .

Patch:
diff --git a/README.md b/README.md
index 3251678..6177998 100644
--- a/README.md
+++ b/README.md
@@ -61,3 +61,4 @@ problems from
 * [Day 46](https://github.com/vaskoz/dailycodingproblem-go/issues/103)
 * [Day 47](https://github.com/vaskoz/dailycodingproblem-go/issues/105)
 * [Day 49](https://github.com/vaskoz/dailycodingproblem-go/issues/107)
+* [Day 50](https://github.com/vaskoz/dailycodingproblem-go/issues/110)
diff --git a/day50/problem.go b/day50/problem.go
new file mode 100644
index 0000000..1366ed5
--- /dev/null
+++ b/day50/problem.go
@@ -0,0 +1,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
+}


Test patch:
diff --git a/day50/problem_test.go b/day50/problem_test.go
new file mode 100644
index 0000000..f9a26fb
--- /dev/null
+++ b/day50/problem_test.go
@@ -0,0 +1,34 @@
+package day50
+
+import "testing"
+
+var testcases = []struct {
+	tree     *ArithmeticTree
+	expected int
+}{
+	{&ArithmeticTree{op: MUL,
+		left:  &ArithmeticTree{op: ADD, left: &ArithmeticTree{val: 3}, right: &ArithmeticTree{val: 2}},
+		right: &ArithmeticTree{op: ADD, left: &ArithmeticTree{val: 4}, right: &ArithmeticTree{val: 5}}},
+		45},
+	{&ArithmeticTree{op: SUB,
+		left:  &ArithmeticTree{op: DIV, left: &ArithmeticTree{val: 100}, right: &ArithmeticTree{val: 2}},
+		right: &ArithmeticTree{op: DIV, left: &ArithmeticTree{val: 100}, right: &ArithmeticTree{val: 5}}},
+		30},
+}
+
+func TestCalculate(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := Calculate(tc.tree); result != tc.expected {
+			t.Errorf("Expected %d got %d", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkCalculate(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			Calculate(tc.tree)
+		}
+	}
+}