Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 326
Good morning! Here's your coding interview problem for today.

This problem was asked by Netflix.

A Cartesian tree with sequence `S` is a binary tree defined by the following two properties:

It is heap-ordered, so that each parent value is strictly less than that of its children.
An in-order traversal of the tree produces nodes with values that correspond exactly to `S`.
For example, given the sequence `[3, 2, 6, 1, 9]`, the resulting Cartesian tree would be:
```
      1
    /   \   
  2       9
 / \
3   6
```
Given a sequence `S`, construct the corresponding Cartesian tree.

Pull request:
Day326
Resolves #661.

Patch:
diff --git a/README.md b/README.md
index dc77d6b..f87cbd5 100644
--- a/README.md
+++ b/README.md
@@ -330,6 +330,7 @@ problems from
 * [Day 323](https://github.com/vaskoz/dailycodingproblem-go/issues/656)
 * [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658)
 * [Day 325](https://github.com/vaskoz/dailycodingproblem-go/issues/660)
+* [Day 326](https://github.com/vaskoz/dailycodingproblem-go/issues/661)
 * [Day 333](https://github.com/vaskoz/dailycodingproblem-go/issues/668)
 * [Day 337](https://github.com/vaskoz/dailycodingproblem-go/issues/674)
 * [Day 338](https://github.com/vaskoz/dailycodingproblem-go/issues/675)
diff --git a/day326/problem.go b/day326/problem.go
new file mode 100644
index 0000000..2d69004
--- /dev/null
+++ b/day326/problem.go
@@ -0,0 +1,30 @@
+package day326
+
+// IntBinaryTree is a typical binary tree
+// that stores integers for a value.
+type IntBinaryTree struct {
+	Value       int
+	Left, Right *IntBinaryTree
+}
+
+// CartesianTree returns the corresponding Cartesian tree
+// based on the given sequence.
+func CartesianTree(s []int) *IntBinaryTree {
+	if len(s) == 0 {
+		return nil
+	}
+
+	minIndex := 0
+
+	for i := range s {
+		if s[i] < s[minIndex] {
+			minIndex = i
+		}
+	}
+
+	root := &IntBinaryTree{s[minIndex], nil, nil}
+	root.Left = CartesianTree(s[:minIndex])
+	root.Right = CartesianTree(s[minIndex+1:])
+
+	return root
+}


Test patch:
diff --git a/day326/problem_test.go b/day326/problem_test.go
new file mode 100644
index 0000000..8ee200c
--- /dev/null
+++ b/day326/problem_test.go
@@ -0,0 +1,54 @@
+package day326
+
+import (
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	seq      []int
+	expected *IntBinaryTree
+}{
+	{
+		[]int{3, 2, 6, 1, 9},
+		&IntBinaryTree{
+			1,
+			&IntBinaryTree{
+				2,
+				&IntBinaryTree{3, nil, nil},
+				&IntBinaryTree{6, nil, nil},
+			},
+			&IntBinaryTree{9, nil, nil},
+		},
+	},
+}
+
+func TestCartesianTree(t *testing.T) {
+	t.Parallel()
+
+	for tcid, tc := range testcases {
+		if result := CartesianTree(tc.seq); !equalTree(result, tc.expected) {
+			t.Errorf("Trees do not match for TCID%d", tcid)
+		}
+	}
+}
+
+func BenchmarkCartesianTree(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			CartesianTree(tc.seq)
+		}
+	}
+}
+
+func equalTree(a, b *IntBinaryTree) bool {
+	if a == nil && b == nil {
+		return true
+	} else if a != nil && b != nil {
+		return a.Value == b.Value &&
+			equalTree(a.Left, b.Left) &&
+			equalTree(a.Right, b.Right)
+	}
+
+	return false
+}