Binary Tree
Learn how trees branch, how to walk them in every order, and how recursion makes tree problems short — then solve the binary tree questions interviews ask most.
- 5 Lessons
- 16 Problems
- 70 Illustrations
What you will learn
- What a binary tree is: roots, children, leaves, depth, height and the LeetCode array format
- Why trees exist, from file systems and the HTML of this page to search that takes 20 steps for a million items
- The kinds of binary trees — full, complete, perfect, balanced, skewed — and the formulas that come with them
- When a problem is a tree problem, and whether it wants depth-first or breadth-first search
- How to think recursively about trees: pass information down, return answers up
- Brute force, optimal and best solutions for 16 interview questions, each with a dry-run simulator
Why this course
Trees are where recursion stops being a trick and becomes the natural way to think: a tree is a root with two smaller trees hanging off it, so almost every tree solution is "solve it for the left subtree, solve it for the right subtree, combine".
The course starts with a deep introduction — what a binary tree is, why it exists, the types of binary trees, when to use one and how to work with one — every idea drawn, and every lesson with a simulator.
Then come the traversals every other problem builds on, the most asked interview questions, and the views of a tree. Every problem goes from brute force to optimal to the best solution, with code in four languages, a simulator that shows the tree changing step by step, and the common variations of the question.
Requirements
- Comfort writing functions, including a function that calls itself
- The Stack course helps: depth-first search is a stack, and breadth-first search is a queue
Course Contents
2. Tree traversals
3. Most asked interview questions
- Maximum Depth of Binary Treecode
solutionEasy - Invert Binary Treecode
solutionEasy - Same Tree & Symmetric Treecode
solutionEasy - Merge Two Binary Treescode
solutionEasy - Average of Levels in Binary Treecode
solutionEasy - Binary Tree Pathscode
solutionEasy - Sum of Left Leavescode
solutionEasy - Sum of Root To Leaf Binary Numberscode
solutionEasy - Subtree of Another Treecode
solutionEasy - Cousins in Binary Treecode
solutionEasy - Path Sum I, II & IIIcode
solutionMedium - Lowest Common Ancestor of a Binary Treecode
solutionMedium
4. Views of a binary tree
Related courses
- Binary Search TreeA binary tree with an ordering invariant that makes search, insert, and delete O(log n).5 lessons · 20 problems
- HeapA tree in an array that keeps the smallest (or largest) value on top — the engine behind priority queues, top-k and streaming medians.4 lessons · 14 problems
- QueueFirst-in, first-out — the structure behind task scheduling and breadth-first traversal.4 lessons · 5 problems
