Pre-order traversal visits root first, then left node, then right node.
Iterative style
voidpre_order(TreeNode*root){auto next = queue<TreeNode*>({root});while(!next.empty()){auto cur = next.front(); next.pop();// Do something to cur
cout << cur->val<< endl;if(cur->left!=nullptr){ next.push(cur->left);}if(cur->right!=nullptr){ next.push(cur->right);}}}
Recursive style
voidpre_order(ListNode*cur){if(cur ==nullptr){return;}else{// Do something to cur
pre_order(cur->left);pre_order(cur->right);}}
In-Order Traversal
This type of traversal is only viable through either a stack or recursive functions.
We push left child to stack if left child is not nullptr, if left child is nullptr, push right child to the stack, and do something to current node and pop current node from stack.
Iterative style
In order to interatively do the post-order traversal, a stack is needed.
if current node is not null, we push current node to stack, and go to left.
if current node is null, we backtrack to top of the stack.
Do something to current node.
Go to right node.
voidin_order(TreeNode*root){auto next = stack<TreeNode*>();auto cur = root;while(!next.empty()|| cur !=nullptr){if(cur !=nullptr){ next.push(cur); cur = cur->left;}else{ cur = next.top(); next.pop();// Do something to cur
cur = cur->right;}}}
Recursive style
voidin_order(TreeNode*cur){if(cur ==nullptr){return;}else{in_order(cur->left);// Do something to cur
in_order(cur->right);}}
Post-Order Traversal
Iterative style
TBD
Recursive style
voidpost_order(TreeNode*cur){if(cur ==nullptr){return;}else{post_order(cur->left);post_order(cur->right);// Do something to cur
}}
Binary Tree BFS
Sometimes the question requires BFS on a binary tree, like print each level from left to right.
Iterative style
voidbfs(TreeNode*root){auto next = queue<TreeNode*>({root});while(!next.empty()){for(auto i =0; i < next.size(); i++){auto cur = next.front(); next.pop();if(cur !=nullptr){// Do something to cur
next.push(cur->left); next.push(cur->right);}}}}
Recursive style
voidbfs(queue<TreeNode*>&next){if(next.empty()){return;}else{for(auto i =0; i < next.size(); i++){auto cur = next.front(); next.pop();if(cur !=nullptr){// Do something to cur
next.push(cur->left); next.push(cur->right);}}bfs(next);}}
This problem requires us to do in-order traversal, and there is a key point of solving this question:
In-order traversal of a BST equals to visiting all elements on BST in sorted order.
Iterative style
intgetMinimumDifference(TreeNode*root){auto next = stack<TreeNode*>();auto cur = root;auto last =-100000;auto result = INT_MAX;while(!next.empty()|| cur !=nullptr){if(cur !=nullptr){ next.push(cur); cur = cur->left;}else{ cur = next.top(); next.pop(); result =min(result, cur->val- last); last = cur->val; cur = cur->right;}}return result;}
Recursive style
intgetMinimumDifference(TreeNode*root){int mind = INT_MAX;int last =-100000;inorderTraversal(root, last, mind);return mind;}voidinorderTraversal(TreeNode*root,int&last,int&mind){if(root ==nullptr){return;}else{inorderTraversal(root->left, last, mind); mind =min(mind, root->val- last); last = root->val;inorderTraversal(root->right, last, mind);}}