Explanation of what a binary tree is, what it means to invert a binary tree and how to do it in Kotlin.

A binary tree is a data structure in which each node has at most two children. The children are referred to as the left child and the right child.

```
1
/ \
(Left Child) 2 3 (Right Child)
/ \
(Left Child) 4 5 (Right Child)
```

To invert, sometimes also called to mirror or reverse, a binary tree is to interchange all left and right nodes.

```
(Original) (Inverted)
1 1
/ \ / \
2 3 3 2
/ \ / \ / \ / \
4 5 6 7 7 6 5 4
/ \
8 8
```

Given that a node has this shape.

```
class TreeNode(var value: Int = 0) {
var left: TreeNode? = null
var right: TreeNode? = null
}
```

Inverting can be done by recursivly walking through the tree and on every level change left for right and right for left.

```
fun invert(root: TreeNode?): TreeNode? {
// Check if we have a node.
if (root == null) {
return null
}
// Invert the sub structures and save the result
// to temporary variables.
val right: TreeNode? = invert(root.right)
val left: TreeNode? = invert(root.left)
// Switch places.
root.left = right
root.right = left
// Return the root.
return root
}
```

- [project-euler] Project Euler Problem 17 - Number Letter Counts - solved with R
- [project-euler] Project Euler Problem 16 - Power Digit Sum - solved with Javascript
- [project-euler] Project Euler Problem 15 - Lattice Paths - solved with R
- [project-euler] Finding the 10001st prime with R
- [project-euler] Find longest Collatz sequence using Javascript
- [project-euler] Sum large numbers with Kotlin versus Javascript
- [tech-test] What is a binary tree and how to invert it using Kotlin
- [project-euler] Find Highly Divisible Triangular Numbers with Kotlin
- [project-euler] Find the Largest Product in a Grid with Rust
- [project-euler] Summation of the First Two Million Primes with Rust
- [project-euler] Finding the largest product in a series with Rust
- [project-euler] Finding the 10001st prime with Rust
- [js] The complete list of rational numbers with Stern-Brocot and Javascript
- [project-euler] Project Euler number six solved with Rust
- [project-euler] Smallest positive number that is evenly divisible by all of the numbers from 1 to 20 with Rust
- [project-euler] Finding the largest palindrome product with Rust
- [project-euler] Get the largest prime factor with Reason
- [project-euler] Find the sum of all even Fibonacci numbers below four million with OCaml
- [project-euler] Finding the sum of all multiples of 3 or 5 below 1000 with OCaml
- [project-euler] Special Pythagorean Triplet solved with Reason
- [personal] New Year's Resolution 2018