Binary Tree Traversal We should handle at least 2 approaches: recursion and iteration.
Preorder
Binary Tree Preorder Traversalhttps://leetcode.com/problems/binary-tree-preorder-traversal/
Recursion
Save root node value
Call recursion on the left children
Call recursion on the right children
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Solution {public : vector<int > preorderTraversal (TreeNode* root) { vector<int > res; preorder (root, res); return res; } void preorder (TreeNode* root, vector<int >& res) { if (!root) return ; res.push_back (root->val); if (root->left) preorder (root->left, res); if (root->right) preorder (root->right, res); } };
Interation
Push root into stack, and save its value
Move pointer to its left node
when pointer is null, means that the node don’t have left child, then back to the top of the stack, move pointer to its right child
Repeat 1-3 until stack or pointer is empty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution {public : vector<int > preorderTraversal (TreeNode* root) { vector<int > res; stack<TreeNode*> stk; TreeNode* p = root; while (!stk.empty () || p) { while (p) { stk.push (p); res.push_back (p->val); p = p->left; } p = stk.top (); stk.pop (); p = p->right; } return res; } };
Inorder
Binary Tree Inorder Traversalhttps://leetcode.com/problems/binary-tree-inorder-traversal/
Recursion
Call recursion on the left child
Push root node value
Call recursion on the right child
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Solution {public : vector<int > inorderTraversal (TreeNode* root) { vector<int > res; inorder (root, res); return res; } void inorder (TreeNode* root, vector<int >& res) { if (!root) return ; if (root->left) inorder (root->left, res); res.push_back (root->val); if (root->right) inorder (root->right, res); } };
Iteration
Push root into stack
Push all left children into stack
Remove the top node from the stack, save node value
Move current pointer to the right child of current node
If contain right child, push all left children into stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution {public : vector<int > inorderTraversal (TreeNode* root) { vector<int > res; stack<TreeNode*> stk; stk.push (root); TreeNode* p = root; while (p || !stk.empty ()) { while (p) { stk.push (p); p->left; } p = stk.top (); stk.pop (); res.push_back (p->val); p->right; } return res; } };
Postorder
Binary Tree Postorder Traversalhttps://leetcode.com/problems/binary-tree-postorder-traversal/
Recursion 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Solution {public : vector<int > postorderTraversal (TreeNode* root) { vector<int > res; postorder (root, res); return res; } void postorder (TreeNode* root, vector<int >& res) { if (!root) return ; if (root->left) postorder (root->left, res); if (root->right) postorder (root->right, res); res.push_back (root->val); } };
Iteration The pointer movement order is: root -> right -> left, push corresponding value to the begining of the res, then the order in res becomes: left -> right -> root.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution {public : vector<int > postorderTraversal (TreeNode* root) { vector<int > res; stack<TreeNode*> stk; TreeNode *p = root; while (!stk.empty () || p) { while (p) { stk.push (p); res.insert (res.begin (), p->val); p = p->right; } TreeNode *t = stk.top (); stk.pop (); p = t->left; } return res; } };