xxxxxxxxxx
# Assuming valid Binary Tree Node
def post_order(node: BinaryTreeNode):
if node.left:
post_order(node.left)
if node.right:
post_order(node.right)
print(node.val)
xxxxxxxxxx
preorder: parent => left => right
inorder: left => parent => right
postorder: left => right => parent
xxxxxxxxxx
<> Preorder Root -> Left -> Right
<> Inorder Left -> Root -> Right
<> Postorder Left -> Right -> Root
xxxxxxxxxx
Hint to Remember:
IN means root is in middle: inorder: left => root => right
PRE means root is at front: preorder: root => left => right
POST means root is at the end: postorder: left => right => root
Always left comes first than right
xxxxxxxxxx
//*******this is a C program for implementation and searching in A BT*******
#include<stdlib.h>
#include <stdio.h>
struct BinaryTree{
int data;
struct BinaryTree*right;
struct BinaryTree*left;
};
struct BinaryTree*createnode(int val){
struct BinaryTree*root=(struct BinaryTree*)malloc(sizeof(struct BinaryTree));
root->data=val;
root->left=NULL;
root->right=NULL;
}
void inorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
}
void preorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {
printf("%d->",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else{
postorder(root->left);
postorder(root->right);
printf("%d->",root->data);
}
}
int main()
{
printf("Lets grow the tree\n");
struct BinaryTree*root=createnode(1);
root->left=createnode(2);
root->right=createnode(3);
root->left->left=createnode(4);
root->left->right=createnode(5);
printf("tree has grown up\n");
printf("Inorder traversal ");
inorder(root);printf("NULL");
printf("\npreorder traversal ");
preorder(root);printf("NULL");
printf("\nPostorder traversal");
postorder(root);printf("NULL");
return 0 ;
}
xxxxxxxxxx
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
print(root, end = " ")
xxxxxxxxxx
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
dfs(root);
return list;
}
public void dfs(TreeNode root)
{
if(root!=null)
{
dfs(root.left);
dfs(root.right);
list.add(root.val);
}
}
}
xxxxxxxxxx
1. Right child of 1 exists.
Push 3 to stack. Push 1 to stack. Move to left child.
Stack: 3, 1
2. Right child of 2 exists.
Push 5 to stack. Push 2 to stack. Move to left child.
Stack: 3, 1, 5, 2
3. Right child of 4 doesn't exist. '
Push 4 to stack. Move to left child.
Stack: 3, 1, 5, 2, 4
4. Current node is NULL.
Pop 4 from stack. Right child of 4 doesn't exist.
Print 4. Set current node to NULL.
Stack: 3, 1, 5, 2
5. Current node is NULL.
Pop 2 from stack. Since right child of 2 equals stack top element,
pop 5 from stack. Now push 2 to stack.
Move current node to right child of 2 i.e. 5
Stack: 3, 1, 2
6. Right child of 5 doesn't exist. Push 5 to stack. Move to left child.
Stack: 3, 1, 2, 5
7. Current node is NULL. Pop 5 from stack. Right child of 5 doesn't exist.
Print 5. Set current node to NULL.
Stack: 3, 1, 2
8. Current node is NULL. Pop 2 from stack.
Right child of 2 is not equal to stack top element.
Print 2. Set current node to NULL.
Stack: 3, 1
9. Current node is NULL. Pop 1 from stack.
Since right child of 1 equals stack top element, pop 3 from stack.
Now push 1 to stack. Move current node to right child of 1 i.e. 3
Stack: 1
10. Repeat the same as above steps and Print 6, 7 and 3.
Pop 1 and Print 1.
xxxxxxxxxx
Inorder Traversal using Stack:
As we already know, recursion can also be implemented using stack. Here also we can use a stack to perform inorder traversal of a Binary Tree. Below is the algorithm for traversing a binary tree using stack.
Create an empty stack (say S).
Initialize the current node as root.
Push the current node to S and set current = current->left until current is NULL
If current is NULL and the stack is not empty then:
Pop the top item from the stack.
Print the popped item and set current = popped_item->right
Go to step 3.