- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Binary Trees Explained
Trees
Binary Trees Explained
A tree is the first structure where recursion stops being a technique and becomes the natural way to think. A tree is a node with two smaller trees hanging off it - that sentence is the whole subject.
The structure
A node, and a tree
javascript
class TreeNode {
constructor(value, left = null, right = null) {
this.value = value
this.left = left
this.right = right
}
}
// 1
// / \
// 2 3
// / \
// 4 5
const root = new TreeNode(1,
new TreeNode(2, new TreeNode(4), new TreeNode(5)),
new TreeNode(3)
)