Binary Tree: Summary Edition! (All the Binary Tree Skills You Need to Master Are Here)

July 28, 2025 · View on GitHub

LeetCode Binary Tree Big Summary!

Unknowingly, the binary tree has been with us for thirty-three days, “Code Thinking Record” has published thirty-three articles on binary trees, providing detailed explanations on 30+ classic binary tree problems. Those who have persisted must have a deep understanding of binary trees.

In each binary tree problem, I have used the three-step method of recursion to analyze the problem. I believe that whenever you see a binary tree, or recursion, you'll think: what are the return values and parameters? What is the termination condition? What is the single-layer logic?

Moreover, I have provided the corresponding iterative method for almost every problem, which can further improve your skills.

Below, Carl categorizes the analyzed problems, which can help new learners to learn binary trees step-by-step, and allow experienced learners to quickly review before interviews. See a title and recall the problem-solving approach; this way, you'll be able to systematically review the binary tree topic quickly.

The posting order of the articles on the public account is gradual, so the categories below roughly follow the article's posting order. I further provide a systematic classification.

Basic Theory of Binary Trees

  • Binary Tree Basics: Types of binary trees, storage methods, traversal methods, definition methods

Traversal Methods of Binary Trees

Calculate Properties of a Binary Tree

  • 0101.Symmetric Tree
    • Recursion: Postorder, compare whether the left and right subtrees of the root node are mirror images of each other
    • Iteration: Use a queue/stack to sequentially put two nodes into the container for comparison
  • 0104.Maximum Depth of Binary Tree
    • Recursion: Postorder, the maximum height of the root node is the maximum depth, calculate tree height through the return value of the recursion function
    • Iteration: Level-order traversal
  • 0111.Minimum Depth of Binary Tree
    • Recursion: Postorder, the minimum height of the root node is the minimum depth; pay attention to the definition of minimum depth
    • Iteration: Level-order traversal
  • 0222.Count Complete Tree Nodes
    • Recursion: Postorder, calculate the number of nodes through the return value of the recursion function
    • Iteration: Level-order traversal
  • 0110.Balanced Binary Tree
    • Recursion: Postorder, note the difference between postorder for height and pre-order for depth, and the recursion process checks the height difference
    • Iteration: Very inefficient, not recommended
  • 0257.Binary Tree Paths
    • Recursion: Preorder, easily lets the parent node point to the child nodes, involves backtracking handling root-to-leaf paths
    • Iteration: One stack simulates recursion, one stack for storing the corresponding traversal path
  • Binary Tree Traversal with Recursion and Backtracking
  • 0404.Sum of Left Leaves
    • Recursion: Postorder, three-layer constraint condition needed to determine if it is a left leaf.
    • Iteration: Direct simulation of postorder traversal
  • 0513.Find Bottom Left Tree Value
    • Recursion: Order doesn't matter, prioritize searching the left child while finding the deepest leaf node.
    • Iteration: Level-order traversal finds the leftmost node of the last row
  • 0112.Path Sum
    • Recursion: Order doesn't matter, the recursion function's return value is bool for searching a single path, no return value for searching the whole tree.
    • Iteration: Stack elements record both node pointers and the path total sum from the head node to that node

Modify and Construct Binary Trees

  • 0226.Invert Binary Tree
    • Recursion: Preorder, swap left and right children
    • Iteration: Directly simulate preorder traversal
  • 0106.Construct Binary Tree from Inorder and Postorder Traversal
    • Recursion: Preorder, the key is to find the partition point and construct the left and right intervals
    • Iteration: Quite complex, not very meaningful
  • 0654.Maximum Binary Tree
    • Recursion: Preorder, partition point is the maximum value of the array, construct the left and right intervals
    • Iteration: Quite complex, not very meaningful
  • 0617.Merge Two Binary Trees
    • Recursion: Preorder, operate on nodes of two trees simultaneously, pay attention to the rules of merging
    • Iteration: Use a queue, similar to level-order traversal

Calculate Properties of a Binary Search Tree

Common Ancestor Problems in Binary Trees

Modifying and Constructing Binary Search Trees

Phase Summary

After completing the above problems, be sure to see the following phase summaries:

Weekly summaries will provide unified answers to everyone's questions and supplement the week's content, so be sure to review them, which will help you capture all scattered knowledge points!

Final Summary

Many students have difficulty choosing a traversal order for binary tree problems. We've done so many binary tree problems; Carl gives a general classification for everyone here.

  • If it involves the construction of a binary tree, whether a regular binary tree or a binary search tree, always use preorder, as the middle node is constructed first.

  • To determine the properties of a regular binary tree, it is typically postorder, as the return values of the recursive function are often used for calculations.

  • To find properties of a binary search tree, it must be inorder, or else its ordering nature is wasted.

Note that in determining the properties of a regular binary tree, I use "typically" for postorder. For instance, achieving depth can also use preorder, and 0257.Binary Tree Paths also used preorder for the parent node to easily point to child nodes.

So, for determining the properties of a regular binary tree, it still requires specific analysis of the specific problem.

Binary Tree Special Topic Gathered into One Chart:

Binary Tree Chart

This chart is created by a code thought blog knowledge planet member: Qing. It summarizes the content quite well, and it's shared with everyone here.

Finally, the binary tree series is perfectly concluded, and it seems this should be the longest series. Thanks for everyone's 33 days of persistence and companionship. We’re going to start a new series, "Backtracking Algorithm"!