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:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# Tree Traversal Methods
def pre_order_traversal(root):
result = []
def traverse(node):
if node is None:
return
# Root, Left, Right
result.append(node.value) # Visit node
traverse(node.left) # Traverse left subtree
traverse(node.right) # Traverse right subtree
traverse(root)
return result
def in_order_traversal(root):
result = []
def traverse(node):
if node is None:
return
# Left, Root, Right
traverse(node.left) # Traverse left subtree
result.append(node.value) # Visit node
traverse(node.right) # Traverse right subtree
traverse(root)
return result
def post_order_traversal(root):
result = []
def traverse(node):
if node is None:
return
# Left, Right, Root
traverse(node.left) # Traverse left subtree
traverse(node.right) # Traverse right subtree
result.append(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:
def __init__(self):
self.root = None
# Insert a value into the BST
def insert(self, value):
new_node = TreeNode(value)
if self.root is None:
self.root = new_node
return self
current = self.root
while True:
# Handle duplicate values (optional)
if value == current.value:
return self
# Go left if value is less than current
if value < current.value:
if current.left is None:
current.left = new_node
return self
current = current.left
# Go right if value is greater than current
else:
if current.right is None:
current.right = new_node
return self
current = current.right
# Search for a value in the BST
def search(self, value):
if self.root is None:
return False
current = self.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.