← All Topics

Binary Trees — Simple English

109 lessons · Data Structures & Algorithms · Simple English · Beginner Friendly

🔒Premium course. The first 3 lessons are free — subscribe to unlock the rest (one plan unlocks every premium course).View plans
01 Understanding a Binary TreeFree Understanding a Binary Tree What is a tree? Think about a family tree, or the way a company is organized: one boss at th… 02 Key Tree TerminologiesFree Key Tree Terminologies A tree is a non-linear data structure. Unlike an array or a linked list, where items sit in one s… 03 Special Types of Binary Trees and Their PropertiesFree Special Types of Binary Trees A binary tree is a tree where each node can have at most two children. But not all binary … 04 Storing a Binary Tree Inside an Array🔒 Storing a Binary Tree Inside an Array When we think of a binary tree, we usually picture circles (nodes) connected by li… 05 Defining a Node in a Binary Tree (Array Version)🔒 What a Node Is in the Array Version of a Binary Tree A binary tree is built out of small pieces called nodes. A node is … 06 Storing a Binary Tree Inside an Array🔒 Storing a Binary Tree Inside an Array We already know how a single node of a binary tree looks when we use an array. Now… 07 Storing Any Binary Tree in an Array (and Why It Gets Wasteful)🔒 Storing Any Binary Tree in an Array (and Why It Gets Wasteful) We already saw that a complete binary tree fits neatly in… 08 How to Store a Binary Tree in Memory Using a Linked List🔒 Storing a Binary Tree in Computer Memory You already know what a tree looks like when you draw it on paper. You know its… 09 Defining a Node in a Binary Tree🔒 Defining a Node in a Binary Tree Every data structure is built out of small, basic units that join together to form the … 10 How Nodes Link Up to Form a Binary Tree🔒 How Nodes Link Up to Form a Binary Tree You already know what a single node looks like. Each node has three parts: a lef… 11 Why Traversing a Tree Is Harder Than a List🔒 Why Traversing a Tree Is Harder Than a List When we traverse a data structure, we simply visit every item in it, one by … 12 Preorder Traversal of a Binary Tree🔒 Preorder Traversal of a Binary Tree A binary tree is a collection of connected boxes called nodes. Each node holds a val… 13 Recursive Preorder Traversal of a Binary Tree🔒 Recursive Preorder Traversal of a Binary Tree A binary tree is a way to store data where each piece of data (called a no… 14 Inorder Traversal of a Binary Tree🔒 Inorder Traversal of a Binary Tree When you have a binary tree (a structure where every node can have a left child and a… 15 Recursive Inorder Traversal of a Binary Tree🔒 Recursive Inorder Traversal of a Binary Tree A binary tree is a way of storing data where each item, called a node, can … 16 Postorder Traversal of a Binary Tree (Left → Right → Root)🔒 Postorder Traversal of a Binary Tree A binary tree is a structure made of nodes, where each node can have a left child a… 17 Postorder Traversal of a Binary Tree (Recursive)🔒 Postorder Traversal of a Binary Tree A binary tree is a structure where each box (called a node) holds a value and can p… 18 Why Recursive Tree Traversal Fails: The Call Stack and Stack Overflow🔒 Why We Need Iterative Tree Traversal Earlier in the course, we learned how to visit every node in a tree using recursion… 19 Iterative Preorder Traversal of a Binary Tree🔒 Iterative Preorder Traversal of a Binary Tree Preorder traversal is a way to visit every node in a binary tree in a fixe… 20 Iterative Preorder Traversal of a Binary Tree🔒 Iterative Preorder Traversal of a Binary Tree A binary tree is a structure made of nodes. Each node holds a value and ca… 21 Iterative Inorder Traversal of a Binary Tree🔒 Iterative Inorder Traversal of a Binary Tree What inorder traversal means A binary tree is a structure where each box (w… 22 Iterative Inorder Traversal of a Binary Tree🔒 Iterative Inorder Traversal of a Binary Tree A binary tree is a structure where each box, called a node, holds a value a… 23 Iterative Postorder Traversal of a Binary Tree🔒 Iterative Postorder Traversal In a binary tree, postorder traversal means we do three things in this exact order for eve… 24 Iterative Postorder Traversal of a Binary Tree🔒 Iterative Postorder Traversal of a Binary Tree A binary tree is a structure made of nodes, where each node can have a le… 25 Level Order Traversal of a Binary Tree🔒 Level Order Traversal of a Binary Tree Imagine you are reading a tree the same way you read a page in a book: one full l… 26 Level Order Traversal of a Binary Tree🔒 Level Order Traversal of a Binary Tree A binary tree is a structure where each box, called a node, holds a value and can… 27 Why Preorder Alone Cannot Rebuild a Tree🔒 Why Preorder Alone Cannot Rebuild a Tree Sometimes we want to save a tree as a simple list of numbers so we can store it… 28 Why Inorder Traversal Alone Cannot Rebuild a Tree🔒 Why Inorder Traversal Alone Cannot Rebuild a Tree Imagine you have a binary tree and you want to save it to a file, send… 29 Why Postorder Traversal Alone Cannot Rebuild a Tree🔒 Why Postorder Traversal Alone Cannot Rebuild a Tree Imagine you have a tree, and you want to save it to a file or send i… 30 Building a Binary Tree from Its Preorder and Inorder Traversals🔒 Building a Binary Tree from Preorder and Inorder A single traversal is usually not enough to know the exact shape of a b… 31 Rebuilding a Binary Tree from Its Preorder and Inorder Lists🔒 Rebuilding a Binary Tree from Its Preorder and Inorder Lists Imagine someone walked through a tree and wrote down the no… 32 Building a Binary Tree from Postorder and Inorder Traversals🔒 Building a Binary Tree from Postorder and Inorder Traversals A traversal is just a list that records the order in which … 33 Building a Binary Tree from Postorder and Inorder Traversals🔒 Building a Binary Tree from Postorder and Inorder Traversals Sometimes you are not given a tree directly. Instead, you a… 34 Inserting a New Node at the Root of a Binary Tree🔒 Inserting a New Node at the Root of a Binary Tree Normally, when we add a value to a tree, we walk down from the top unt… 35 Insert a New Node at the Root of a Binary Tree🔒 Insert a New Node at the Root of a Binary Tree A binary tree is a way of storing data where every item, called a node, c… 36 Inserting a New Node as a Leaf in a Binary Tree (Recursion)🔒 Inserting a New Node as a Leaf in a Binary Tree A binary tree is made of nodes. Each node holds a value and can point to… 37 Inserting a New Leaf Node into a Binary Tree (Recursively)🔒 Inserting a New Leaf Node into a Binary Tree A binary tree is a structure where each box (we call it a node) holds a val… 38 Inserting a Leaf Node Using Level Order Traversal (Iterative)🔒 Inserting a Leaf Node the Iterative Way Earlier we saw how to add a new node to a binary tree using recursion. That meth… 39 Inserting a New Leaf into a Binary Tree Using Level-Order (BFS)🔒 Inserting a New Leaf into a Binary Tree A binary tree is a structure where each node can have up to two children: a left… 40 Inserting a New Node as the Child of a Given Node in a Binary Tree🔒 Inserting a Child Node in a Binary Tree So far you may have learned how to add a node at the very top of a tree (the roo… 41 Inserting a New Node as the Left Child in a Binary Tree🔒 Inserting a New Node as a Left Child Imagine a family tree, but with a strict rule: every person can have at most two ch… 42 Inserting a New Node as a Parent in a Binary Tree🔒 Inserting a New Node as a Parent Most of the time when we add a node to a binary tree, we hang it at the bottom as a chi… 43 Inserting a New Node as a Parent in a Binary Tree🔒 Inserting a New Node as a Parent in a Binary Tree Imagine a family tree where each person hangs below their parent. A bi… 44 Stateless Preorder Traversal: Passing Data from Parent to Child🔒 Stateless Preorder Traversal: Passing Data Down the Tree When we walk through a binary tree, the order in which we visit… 45 The Stateless Preorder Traversal Pattern🔒 The Stateless Preorder Traversal Pattern Imagine information flowing downhill in a tree — from the top (the root) toward… 46 Sum of Path: Adding Up Ancestor Values in a Binary Tree🔒 Sum of Path: Adding Up Ancestor Values Imagine a family tree. Every person has a chain of parents, grandparents, and so … 47 Setting Each Tree Node to Its Depth🔒 Setting Each Tree Node to Its Depth In this problem, you are given a binary tree and asked to change every node so that … 48 Concatenated Path: Building Numbers Along a Tree Path🔒 Concatenated Path: Building Numbers Along a Tree Path Let's solve a fun problem about binary trees. A binary tree is a s… 49 Increasing Path: Marking Nodes on a Strictly Rising Root-to-Node Path🔒 Increasing Path in a Binary Tree Imagine a tree where you start at the very top node (called the root) and walk down to … 50 The Stateful Preorder Traversal Pattern🔒 The Stateful Preorder Traversal Pattern Quick reminder: what is preorder? In a binary tree, preorder traversal visits no… 51 The Stateful Preorder Traversal Pattern🔒 The Stateful Preorder Traversal Pattern Some binary tree problems share the same shape. Once you learn to spot that shap… 52 Counting Nodes Whose Path Already Contains Their Value🔒 Duplicates in a Root-to-Node Path What the problem is asking Imagine a binary tree. A binary tree is just a set of conne… 53 Finding the Second Smallest Value in a Binary Tree🔒 Finding the Second Smallest Value in a Binary Tree Let's start with a simple goal: we are given a tree of numbers, and w… 54 Left View of a Binary Tree🔒 Left View of a Binary Tree Imagine you are standing on the left side of a tree and looking at it. Some nodes are hidden … 55 Right View of a Binary Tree🔒 Right View of a Binary Tree Imagine you are standing on the right side of a tree and looking straight across at it. Some… 56 The Stateless Postorder Traversal Pattern🔒 The Stateless Postorder Traversal Pattern When we work with a binary tree, every node can have a left child and a right … 57 The Stateless Postorder Traversal Pattern🔒 The Stateless Postorder Traversal Pattern Many binary tree problems share the same shape. Once you learn to recognize th… 58 Sum of Leaf Nodes in a Binary Tree🔒 Sum of Leaf Nodes in a Binary Tree In this lesson we solve a common tree problem: add up the values of all the leaf node… 59 Height of a Binary Tree🔒 Height of a Binary Tree A binary tree is a way to store data where each item, called a node, can point to at most two ot… 60 Maximum Root-to-Leaf Path Sum in a Binary Tree🔒 Maximum Path Sum (Root to Leaf) In this problem we are given a binary tree and asked to find the largest possible sum we… 61 Full Binary Tree: Every Node Has Two Children or None🔒 Full Binary Tree Before we solve this problem, let's understand what a binary tree is. A binary tree is a way of organiz… 62 What Is a Perfect Binary Tree?🔒 What Is a Perfect Binary Tree? A binary tree is a structure where each item, called a node, can point to at most two oth… 63 Collect Leaves of a Binary Tree, Layer by Layer🔒 Collect Leaves of a Binary Tree Imagine a tree made of nodes. Each node holds a number and can have a left child and a r… 64 The Stateful Postorder Traversal Pattern🔒 The Stateful Postorder Traversal Pattern When we solve problems on a binary tree, we often work from the bottom up. We c… 65 The Stateful Postorder Traversal Pattern (Tree Diameter)🔒 The Stateful Postorder Traversal Pattern There is a whole family of binary tree problems that all look different on the … 66 Diameter of a Binary Tree🔒 Diameter of a Binary Tree A binary tree is a structure where each item, called a node, can have up to two children: a le… 67 Counting Nodes Whose Value Equals the Sum of Their Descendants🔒 Counting Nodes Whose Value Equals the Sum of Their Descendants Imagine a family tree. Every person sits above the people… 68 Distribute Coins in a Binary Tree🔒 Distribute Coins in a Binary Tree Imagine you have a tree of people. Each person is holding some coins, and the rule is … 69 Most Frequent Subtree Sum🔒 Most Frequent Subtree Sum This lesson teaches a classic problem on binary trees. Before we solve it, let's make sure we … 70 Finding the Longest Monotonic Path in a Binary Tree🔒 Finding the Longest Monotonic Path in a Binary Tree Let's start with a quick picture of what a binary tree is. Think of … 71 Counting Monotonic Subtrees in a Binary Tree🔒 Counting Monotonic Subtrees Imagine a family tree, but for numbers. A binary tree is a structure where each box (we call… 72 Counting Downward Paths in a Binary Tree That Add Up to a Target🔒 Counting Downward Paths That Sum to a Target Imagine a family tree where each person holds a number. Starting from any p… 73 The Stateless Root-to-Leaf Path Pattern in Binary Trees🔒 The Stateless Root-to-Leaf Path Pattern What is a root-to-leaf path? In a binary tree, a root-to-leaf path is the chain … 74 The Stateless Root-to-Leaf Path Pattern🔒 The Stateless Root-to-Leaf Path Pattern A binary tree is like a family tree turned upside down. The top node is the root… 75 Root-to-Leaf Path Sum in a Binary Tree🔒 Root-to-Leaf Path Sum Imagine a tree of numbers. You start at the very top node (called the root) and walk downward, ste… 76 Binary Summation of a Tree: Adding Up Root-to-Leaf Numbers🔒 Binary Summation of a Tree Imagine a tree where every node holds just a single digit: either 0 or 1. Nothing else. This … 77 Even Path in a Binary Tree🔒 Even Path in a Binary Tree Before we solve this problem, let's make sure we understand the words in it. What is a binary… 78 Counting Root-to-Leaf Paths with an Odd Number of Nodes🔒 Counting Root-to-Leaf Paths with an Odd Number of Nodes A binary tree is a way to store data where each item, called a n… 79 The Stateful Root-to-Leaf Path Pattern🔒 The Stateful Root-to-Leaf Path Pattern A lot of binary tree problems share the same shape. You walk down every path from… 80 The Stateful Root-to-Leaf Path Pattern in Binary Trees🔒 The Stateful Root-to-Leaf Path Pattern Many binary tree problems ask the same kind of question: walk from the root (the … 81 Finding Root-to-Leaf Paths That Add Up to a Target🔒 Finding Root-to-Leaf Paths That Add Up to a Target A binary tree is a structure made of connected boxes called nodes. Ea… 82 Finding Root-to-Leaf Paths with Equal Even and Odd Nodes🔒 Equal Paths in a Binary Tree This is a classic tree problem. Before we solve it, let's make sure we understand every wor… 83 Finding Duplicate Root-to-Leaf Paths in a Binary Tree🔒 Finding Duplicate Root-to-Leaf Paths in a Binary Tree Let's learn how to solve a coding problem called Duplicate paths. … 84 Finding Root-to-Leaf Paths Whose Total Equals a Prefix Total🔒 Prefix Paths in a Binary Tree Let's learn a tree problem step by step. First, a quick reminder of the words we will use.… 85 Level Order Traversal: Visiting a Tree One Level at a Time🔒 Level Order Traversal: Visiting a Tree One Level at a Time Imagine a binary tree like a small family chart. At the top s… 86 Level Order Traversal: Solving Level-by-Level Problems🔒 Level Order Traversal: A Pattern for Level-by-Level Problems Some binary tree problems are not about going deep down one… 87 Level Sum of a Binary Tree🔒 Level Sum of a Binary Tree A binary tree is a structure made of connected boxes called nodes. Each node holds a number a… 88 Deepest Leaves Sum of a Binary Tree🔒 Deepest Leaves Sum Imagine a family tree drawn upside down. At the very top sits one person, the root. Below it branch o… 89 Checking if a Binary Tree is Complete🔒 Complete Binary Trees A binary tree is a structure where each box (called a node) holds a value and can point to up to t… 90 Zigzag Traversal of a Binary Tree🔒 Zigzag Traversal of a Binary Tree When we want to look at every node in a binary tree, we usually go level by level. We … 91 Cousins in a Binary Tree🔒 Cousins in a Binary Tree Before we solve this problem, let's understand the words we need. A binary tree is a way to sto… 92 Top View of a Binary Tree🔒 Top View of a Binary Tree Imagine you are standing directly above a tree and looking straight down at it. You cannot see… 93 Bottom View of a Binary Tree🔒 Bottom View of a Binary Tree Imagine you are lying on the floor underneath a tree and looking straight up. Some leaves b… 94 Vertical Traversal of a Binary Tree🔒 Vertical Traversal of a Binary Tree Imagine you are looking at a binary tree from above, and you draw straight up-and-do… 95 Diagonal Traversal of a Binary Tree🔒 Diagonal Traversal of a Binary Tree Imagine standing above a binary tree and shining a flashlight so the light comes in … 96 The Lowest Common Ancestor Pattern in Binary Trees🔒 What is the Lowest Common Ancestor? Imagine a family tree. Two people can have many shared ancestors — parents, grandpar… 97 The Lowest Common Ancestor Pattern: How to Spot It and Solve It🔒 The Lowest Common Ancestor Pattern When you work with binary trees, many problems start to look the same after a while. … 98 Lowest Common Ancestor of Two Nodes in a Binary Tree🔒 Lowest Common Ancestor in a Binary Tree Imagine a family tree. Every person has a position, and people connect to each o… 99 Lowest Common Ancestor of Two Nodes in a Binary Tree🔒 Lowest Common Ancestor (LCA) Imagine a family tree. Each person has children, and those children have children of their … 100 Lowest Common Ancestor of Several Random Nodes🔒 Lowest Common Ancestor of Several Random Nodes Imagine a family tree. Every person has a parent above them, and below th… 101 Finding the Lowest Common Ancestor of the Deepest Leaves🔒 Finding the Lowest Common Ancestor of the Deepest Leaves Imagine a family tree turned upside down. The oldest person sit… 102 Finding the Distance Between Two Nodes in a Binary Tree🔒 Finding the Distance Between Two Nodes in a Binary Tree Imagine a family tree drawn on paper. Each person is a "box," an… 103 Simultaneous Traversal: Walking Two Binary Trees at the Same Time🔒 Walking Two Binary Trees at the Same Time Most of the time when we work with a binary tree, we visit its nodes one by on… 104 Simultaneous Traversal: Walking Two Binary Trees at the Same Time🔒 Simultaneous Traversal: Walking Two Trees Together Sometimes you are given two binary trees instead of one, and you need… 105 Checking If Two Binary Trees Are Identical🔒 Checking If Two Binary Trees Are Identical Imagine you have two family trees drawn on paper. You want to know if they ar… 106 Checking if a Binary Tree is Symmetric (a Mirror of Itself)🔒 Checking if a Binary Tree is Symmetric Imagine you hold a piece of paper up to a mirror. If the left side and the right … 107 Subtree Detection: Is One Tree Hiding Inside Another?🔒 Subtree Detection: Is One Tree Hiding Inside Another? Imagine you have a big family tree, and a friend shows you a small… 108 Merging Two Binary Trees🔒 Merging Two Binary Trees Sometimes we have two trees and we want to combine them into a single new tree. Let's learn how… 109 Boundary Traversal of a Binary Tree🔒 Boundary Traversal of a Binary Tree Imagine you have a binary tree drawn on paper. Now picture taking a pen and tracing …