Learn the fundamentals of binary trees, their properties, and basic operations. Master the essential concepts that form the foundation of tree data structures.
A binary tree is a hierarchical data structure consisting of nodes, where each node has at most two children (left and right). Binary trees are used in various applications from expressing hierarchical relationships to implementing efficient searching and sorting algorithms.
Tree traversal is the process of visiting each node in a tree data structure exactly once. There are several ways to traverse a binary tree:
// Binary Tree Node
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
// Tree Traversal Methods
function preOrderTraversal(root) {
const result = [];
function traverse(node) {
if (node === null) return;
// Root, Left, Right
result.push(node.value); // Visit node
traverse(node.left); // Traverse left subtree
traverse(node.right); // Traverse right subtree
}
traverse(root);
return result;
}
function inOrderTraversal(root) {
const result = [];
function traverse(node) {
if (node === null) return;
// Left, Root, Right
traverse(node.left); // Traverse left subtree
result.push(node.value); // Visit node
traverse(node.right); // Traverse right subtree
}
traverse(root);
return result;
}
function postOrderTraversal(root) {
const result = [];
function traverse(node) {
if (node === null) return;
// Left, Right, Root
traverse(node.left); // Traverse left subtree
traverse(node.right); // Traverse right subtree
result.push(node.value); // Visit node
}
traverse(root);
return result;
}
A Binary Search Tree is a special type of binary tree with the following properties:
The BST property allows for efficient searching, insertion, and deletion operations with an average time complexity of O(log n) for a balanced tree.
class BinarySearchTree {
constructor() {
this.root = null;
}
// Insert a value into the BST
insert(value) {
const newNode = new TreeNode(value);
if (this.root === null) {
this.root = newNode;
return this;
}
let current = this.root;
while (true) {
// Handle duplicate values (optional)
if (value === current.value) return this;
// Go left if value is less than current
if (value < current.value) {
if (current.left === null) {
current.left = newNode;
return this;
}
current = current.left;
}
// Go right if value is greater than current
else {
if (current.right === null) {
current.right = newNode;
return this;
}
current = current.right;
}
}
}
// Search for a value in the BST
search(value) {
if (this.root === null) return false;
let current = this.root;
while (current) {
if (value === current.value) return true;
// Decide whether to go left or right
if (value < current.value) {
current = current.left;
} else {
current = current.right;
}
}
return false; // Value not found
}
}
class BinaryTreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
This basic implementation includes:
def in_order_traversal(node):
if node:
in_order_traversal(node.left)
print(node.value)
in_order_traversal(node.right)
def pre_order_traversal(node):
if node:
print(node.value)
pre_order_traversal(node.left)
pre_order_traversal(node.right)
def post_order_traversal(node):
if node:
post_order_traversal(node.left)
post_order_traversal(node.right)
print(node.value)
def insert(root, value):
if root is None:
return BinaryTreeNode(value)
if value < root.value:
root.left = insert(root.left, value)
else:
root.right = insert(root.right, value)
return root
def search(root, value):
if root is None or root.value == value:
return root
if value < root.value:
return search(root.left, value)
return search(root.right, value)
Write a function to calculate the height of a binary tree.
Input:
1
/ \
2 3
/ \
4 5
Output: 3
Implement level-order traversal using a queue.
Input:
1
/ \
2 3
/ \
4 5
Output: [1, 2, 3, 4, 5]
Implement a function to check if a binary tree is balanced (the height difference between left and right subtrees is not more than 1 for all nodes).
Balanced Tree:
1
/ \
2 3
/ \
4 5
Not Balanced:
1
/
2
/
3
/
4
Practice with these recommended LeetCode problems to strengthen your binary tree skills:
Note: Previously, this course referenced the CodeSignal Arcade, which is no longer available. The LeetCode problems above follow the same principles and are an excellent alternative for practicing binary tree algorithms and preparing for technical interviews.