Theory
A binary tree is a tree data structure in which each node can have at most two children, commonly called the left child and right child. Binary trees are important for understanding binary search trees and tree traversal.
Example
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
console.log(root.value);
Expected Output
10
Practice
Create a binary tree with a root and two children.
Interview Questions
- What is a binary tree?
- How many children can a binary-tree node have?
- What are left and right children?
- What is a leaf?