刷题总结 - Binary Tree
A tree is a frequently-used data structure to simulate a hierarchical tree structure.
Each node of the tree will have a root value and a list of references to other nodes which are called child nodes. From graph view, a tree can also be defined as a directed acyclic graph which has N nodes and N-1 edges.
A Binary Tree is one of the most typical tree structure. As the name suggests, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.
By completing this card, you will be able to:
- Understand the concept of a
treeand abinary tree; - Be familiar with different traversal methods;
- Use recursion to solve binary-tree-related problems;
Traverse A Tree
In the introduction, we have gone through the concept of a tree and a binary tree.
In this chapter, we will focus on the traversal methods used in a binary tree. Understanding these traversal methods will definitely help you have a better understanding of the tree structure and have a solid foundation for the further study.
The goal of this chapter is to:
- Understand the difference between different tree traversal methods;
- Be able to solve preorder, inorder and postorder traversal recursively;
- Be able to solve preorder, inorder and postorder traversal iteratively;
- Be able to do level traversal using BFS.
Traverse a Tree - Introduction
Pre-order Traversal
Pre-order traversal is to visit the root first. Then traverse the left subtree. Finally, traverse the right subtree.
In-order Traversal
In-order traversal is to traverse the left subtree first. Then visit the root. Finally, traverse the right subtree.
Typically, for binary search tree, we can retrieve all the data in sorted order using in-order traversal. We will mention that again in another card(Introduction to Data Structure - Binary Search Tree).
Post-order Traversal
Post-order traversal is to traverse the left subtree first. Then traverse the right subtree. Finally, visit the root.
It is worth noting that when you delete nodes in a tree, deletion process will be in post-order. That is to say, when you delete a node, you will delete its left child and its right child before you delete the node itself.
Also, post-order is widely use in mathematical expression. It is easier to write a program to parse a post-order expression. Here is an example:

You can easily figure out the original expression using the inorder traversal. However, it is not easy for a program to handle this expression since you have to check the priorities of operations.
If you handle this tree in postorder, you can easily handle the expression using a stack. Each time when you meet a operator, you can just pop 2 elements from the stack, calculate the result and push the result back into the stack.
Recursive or Iterative
Try to practice the three different traversal methods in our after-article exercise. You might want to implement the methods recursively or iteratively. Implement both recursion and iteration solutions and compare the differences between them.
Question - Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
1 | // Recursive Without Helper Function |
1 | // Recursive With Helper Function |
1 | // Iterative |
Question - Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
1 | // Recursive Without Helper Function |
1 | // Recursive With Helper Function |
1 | // Iterative |
Question - Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
1 | // Iterative |
