In this article we will discuss two approaches to do binary tree traversal: recursion based and stack based.
In general, these two approach have their own strengths and weaknesses. The recursion ones are easy to understand and implement however might cause stack overflow. The stack based are more efficient in terms of memory but hard to understand and more complex to implement compared to recursion based approach. Normally on a coding interview, the interviewer prefers stack based binary tree traversal.
We will discuss four types of binary tree traversal:
- Preorder Traversal https://leetcode.com/problems/binary-tree-preorder-traversal/
- Inorder Traversal https://leetcode.com/problems/binary-tree-inorder-traversal/
- Postorder Traversal https://leetcode.com/problems/binary-tree-postorder-traversal/
- Level Order Traversal https://leetcode.com/problems/binary-tree-level-order-traversal/
Binary Tree Preorder Traversal
Recursion based
1 | class Solution: |
Stack based:
How to derive a stack-based solution?
For preorder traversal, it’s root->left->right. So for any node, we first get its value and then check if it has left child. If so, we goes into left child. Until it’s none. If it’s none, we want to check it’s right child.
Remember after visited left child we want to visit its right child. That why we need stack.
“For each node, it is root to itself.”
For any node the detailed steps are:
1) Get its value, store the node into stack and goes to its left child
2) If left child is none then pop the stack and goes into its right child. If left child is valid then continue step 1
Universal one:
1 | class Solution: |
Special yet simple one:
1 | class Solution: |
Binary Tree Inorder Traversal
Recursion based:
1 | class Solution: |
Stack based:
How to derive a stack-based solution?
For inorder traversal, it’s left->root->right. So for any node, we check if it has left child. If so, we goes into left child. Until it’s none. If it’s none, we want to get back to node gets its value and check it’s right child.
Remember after visited left child we want to visit itself.
“For each node, it is root to itself.”
For any node the detailed steps are:
1) Store the node into stack and goes to its left child
2) If left child is none then pop the stack, gets the node’s value and goes into its right child. If left child is valid then continue step 1
1 | class Solution: |
Binary Tree Postorder Traversal
Recursion based:
1 | class Solution: |
Stack based:
How to derive a stack-based solution?
Can’t use the universal template for preorder and inorder. Because it’s left->right->root, the root is the last one to visit. It’s useless to use stack. Because when we pop a node from stack, it’s root to itself.
This one is harder than others. But we have a tricky solution. We just traverse tree in root-right-left order. And return the reversed result is the correct answer!
1 | class Solution: |
Binary Tree Level Order Traversal
This one we use queue and breadth first search instead.
Recommend you look https://lavidal.github.io/2020/08/06/Breadth-First-Search/ for the idea and solution!