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 (Java)
class TreeNode {
int value;
TreeNode left, right;
TreeNode(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}
// Tree Traversal Methods (Java)
public static List<Integer> preOrderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
preOrderHelper(root, result);
return result;
}
private static void preOrderHelper(TreeNode node, List<Integer> result) {
if (node == null) return;
result.add(node.value); // Visit node
preOrderHelper(node.left, result);
preOrderHelper(node.right, result);
}
public static List<Integer> inOrderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inOrderHelper(root, result);
return result;
}
private static void inOrderHelper(TreeNode node, List<Integer> result) {
if (node == null) return;
inOrderHelper(node.left, result);
result.add(node.value); // Visit node
inOrderHelper(node.right, result);
}
public static List<Integer> postOrderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
postOrderHelper(root, result);
return result;
}
private static void postOrderHelper(TreeNode node, List<Integer> result) {
if (node == null) return;
postOrderHelper(node.left, result);
postOrderHelper(node.right, result);
result.add(node.value); // Visit node
}
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.
// Binary Search Tree (Java)
class BinarySearchTree {
TreeNode root;
public BinarySearchTree() {
this.root = null;
}
// Insert a value into the BST
public void insert(int value) {
root = insertRec(root, value);
}
private TreeNode insertRec(TreeNode root, int value) {
if (root == null) return new TreeNode(value);
if (value < root.value) root.left = insertRec(root.left, value);
else if (value > root.value) root.right = insertRec(root.right, value);
return root;
}
// Search for a value in the BST
public boolean search(int value) {
return searchRec(root, value) != null;
}
private TreeNode searchRec(TreeNode root, int value) {
if (root == null || root.value == value) return root;
if (value < root.value) return searchRec(root.left, value);
return searchRec(root.right, value);
}
}
// Binary Tree Node (Java)
class BinaryTreeNode {
int value;
BinaryTreeNode left, right;
BinaryTreeNode(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}
This basic implementation includes:
// In-order Traversal (Java)
public static void inOrderTraversal(BinaryTreeNode node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.println(node.value);
inOrderTraversal(node.right);
}
}
// Pre-order Traversal (Java)
public static void preOrderTraversal(BinaryTreeNode node) {
if (node != null) {
System.out.println(node.value);
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
// Post-order Traversal (Java)
public static void postOrderTraversal(BinaryTreeNode node) {
if (node != null) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.println(node.value);
}
}
// Insert into BST (Java)
public static BinaryTreeNode insert(BinaryTreeNode root, int value) {
if (root == null) return new BinaryTreeNode(value);
if (value < root.value) root.left = insert(root.left, value);
else root.right = insert(root.right, value);
return root;
}
// Search in BST (Java)
public static BinaryTreeNode search(BinaryTreeNode root, int value) {
if (root == null || 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.