Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 394
Good morning! Here's your coding interview problem for today.

This problem was asked by Uber.

Given a binary tree and an integer `k`, return whether there exists a root-to-leaf path that sums up to k.

For example, given `k = 18` and the following binary tree:
```
    8
   / \
  4   13
 / \   \
2   6   19
```
Return `True` since the path `8 -> 4 -> 6` sums to `18`.

Pull request description:
Day394
Resolves #791.

Patch:
diff --git a/README.md b/README.md
index b70ef7e..43dc9ed 100644
--- a/README.md
+++ b/README.md
@@ -386,4 +386,5 @@ problems from
 * [Day 391](https://github.com/vaskoz/dailycodingproblem-go/issues/785)
 * [Day 392](https://github.com/vaskoz/dailycodingproblem-go/issues/787)
 * [Day 393](https://github.com/vaskoz/dailycodingproblem-go/issues/789)
+* [Day 394](https://github.com/vaskoz/dailycodingproblem-go/issues/791)
 
diff --git a/day394/problem.go b/day394/problem.go
new file mode 100644
index 0000000..180aea2
--- /dev/null
+++ b/day394/problem.go
@@ -0,0 +1,29 @@
+package day394
+
+// IntBinaryTree is a binary tree of integers.
+type IntBinaryTree struct {
+	Value       int
+	Left, Right *IntBinaryTree
+}
+
+// RootToLeafTarget answers true/false if there is a path
+// that sums to target from root to leaf.
+func RootToLeafTarget(root *IntBinaryTree, target int) bool {
+	if root.Left == nil && root.Right == nil {
+		return target == root.Value
+	}
+
+	if root.Left != nil {
+		if ans := RootToLeafTarget(root.Left, target-root.Value); ans {
+			return ans
+		}
+	}
+
+	if root.Right != nil {
+		if ans := RootToLeafTarget(root.Right, target-root.Value); ans {
+			return ans
+		}
+	}
+
+	return false
+}


Test patch:
diff --git a/day394/problem_test.go b/day394/problem_test.go
new file mode 100644
index 0000000..9311489
--- /dev/null
+++ b/day394/problem_test.go
@@ -0,0 +1,97 @@
+package day394
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	root     *IntBinaryTree
+	target   int
+	expected bool
+}{
+	{
+		&IntBinaryTree{
+			8,
+			&IntBinaryTree{
+				4,
+				&IntBinaryTree{2, nil, nil},
+				&IntBinaryTree{6, nil, nil},
+			},
+			&IntBinaryTree{
+				13,
+				nil,
+				&IntBinaryTree{19, nil, nil},
+			},
+		},
+		18,
+		true,
+	},
+	{
+		&IntBinaryTree{
+			8,
+			&IntBinaryTree{
+				4,
+				&IntBinaryTree{2, nil, nil},
+				&IntBinaryTree{6, nil, nil},
+			},
+			&IntBinaryTree{
+				13,
+				nil,
+				&IntBinaryTree{19, nil, nil},
+			},
+		},
+		100,
+		false,
+	},
+	{
+		&IntBinaryTree{
+			8,
+			&IntBinaryTree{
+				4,
+				&IntBinaryTree{2, nil, nil},
+				&IntBinaryTree{6, nil, nil},
+			},
+			&IntBinaryTree{
+				13,
+				nil,
+				&IntBinaryTree{19, nil, nil},
+			},
+		},
+		21,
+		false,
+	},
+	{
+		&IntBinaryTree{
+			8,
+			&IntBinaryTree{
+				4,
+				&IntBinaryTree{2, nil, nil},
+				&IntBinaryTree{6, nil, nil},
+			},
+			&IntBinaryTree{
+				13,
+				nil,
+				&IntBinaryTree{19, nil, nil},
+			},
+		},
+		40,
+		true,
+	},
+}
+
+func TestRootToLeafTarget(t *testing.T) {
+	t.Parallel()
+
+	for tcid, tc := range testcases {
+		if res := RootToLeafTarget(tc.root, tc.target); res != tc.expected {
+			t.Errorf("TCID%d expected %v, got %v", tcid, tc.expected, res)
+		}
+	}
+}
+
+func BenchmarkRootToLeafTarget(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			RootToLeafTarget(tc.root, tc.target)
+		}
+	}
+}