Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 254
Good morning! Here's your coding interview problem for today.

This problem was asked by Yahoo.

Recall that a full binary tree is one in which each node is either a leaf node, or has two children. Given a binary tree, convert it to a full one by removing nodes with only one child.

For example, given the following tree:
```
         0
      /     \
    1         2
  /            \
3                 4
  \             /   \
    5          6     7
```
You should convert it to:
```
     0
  /     \
5         4
        /   \
       6     7
```

Pull request:
Day254
Resolves #523.

Patch:
diff --git a/README.md b/README.md
index 62b0659..c0251d9 100644
--- a/README.md
+++ b/README.md
@@ -264,3 +264,4 @@ problems from
 * [Day 251](https://github.com/vaskoz/dailycodingproblem-go/issues/517)
 * [Day 252](https://github.com/vaskoz/dailycodingproblem-go/issues/519)
 * [Day 253](https://github.com/vaskoz/dailycodingproblem-go/issues/521)
+* [Day 254](https://github.com/vaskoz/dailycodingproblem-go/issues/523)
diff --git a/day254/problem.go b/day254/problem.go
new file mode 100644
index 0000000..4d2988c
--- /dev/null
+++ b/day254/problem.go
@@ -0,0 +1,30 @@
+package day254
+
+// BinaryTree with an int value and a left and right pointer.
+type BinaryTree struct {
+	Value       int
+	Left, Right *BinaryTree
+}
+
+// ConvertBinaryTreeToFull deletes nodes in a binary tree
+// until it's a full tree.
+func ConvertBinaryTreeToFull(root *BinaryTree) *BinaryTree {
+	if root.Left == nil && root.Right == nil {
+		return root
+	}
+	var left, right *BinaryTree
+	if root.Left != nil {
+		left = ConvertBinaryTreeToFull(root.Left)
+	}
+	if root.Right != nil {
+		right = ConvertBinaryTreeToFull(root.Right)
+	}
+	if left != nil && right != nil {
+		root.Left = left
+		root.Right = right
+		return root
+	} else if left != nil {
+		return left
+	}
+	return right
+}


Test patch:
diff --git a/day254/problem_test.go b/day254/problem_test.go
new file mode 100644
index 0000000..0dfdc86
--- /dev/null
+++ b/day254/problem_test.go
@@ -0,0 +1,74 @@
+package day254
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	input, expected *BinaryTree
+}{
+	{
+		&BinaryTree{
+			0,
+			&BinaryTree{
+				1,
+				&BinaryTree{
+					3,
+					nil,
+					&BinaryTree{5, nil, nil},
+				},
+				nil,
+			},
+			&BinaryTree{
+				2,
+				nil,
+				&BinaryTree{
+					4,
+					&BinaryTree{6, nil, nil},
+					&BinaryTree{7, nil, nil},
+				},
+			},
+		},
+		&BinaryTree{
+			0,
+			&BinaryTree{5, nil, nil},
+			&BinaryTree{
+				4,
+				&BinaryTree{6, nil, nil},
+				&BinaryTree{7, nil, nil},
+			},
+		},
+	},
+}
+
+func TestConvertBinaryTreeToFull(t *testing.T) {
+	t.Parallel()
+	for tcid, tc := range testcases {
+		tree := copyTree(tc.input)
+		if result := ConvertBinaryTreeToFull(tree); !equalTree(result, tc.expected) {
+			t.Errorf("Trees don't match for tcid%d", tcid)
+		}
+	}
+}
+
+func copyTree(a *BinaryTree) *BinaryTree {
+	if a == nil {
+		return nil
+	}
+	n := &BinaryTree{}
+	n.Value = a.Value
+	n.Left = copyTree(a.Left)
+	n.Right = copyTree(a.Right)
+	return n
+}
+
+func equalTree(a, b *BinaryTree) bool {
+	if a == nil && b == nil {
+		return true
+	}
+	if a != nil && b != nil {
+		return a.Value == b.Value &&
+			equalTree(a.Left, b.Left) &&
+			equalTree(a.Right, b.Right)
+	}
+	return false
+}