Skip to main content

Tree Traversals Made Easy!

In this blog, I'm going to discuss one of the easiest ways by which you can traverse a binary tree.

Structure Of Tree

struct node
{
    int data;
    node *right,*left;
    node(int data)
    {
        this->data=data;
        left=right=NULL;
    }
};

Tree traversal 

Pre-order :  Root -> Left -> Right 

void preorder(node* root)
{
    if(root==NULL) // base condition
        return ;
        cout<data<<" "; // print root
    preorder(root->left); // go to left child
    preorder(root->right); // go to right child
}

In-order : Left -> Root -> Right

void inorder(node* root)
{
    if(root==NULL)
        return ;
    inorder(root->left);
    cout<<root->data<<" ";
    inorder(root->right);
}

Post-order : Left -> Right -> Root 

void postorder(node* root)
{
    if(root==NULL)
        return ;
    postorder(root->left);
        postorder(root->right);
        cout<<root->data<<" ";
}

Sample Main Function
/*
int main() 
{ 
node* root = new node(12); 
root->left = new node(10); 
root->right = new node(30); 
root->right->left = new node(25); 
root->right->right = new node(40); 
inorder(root);
        preorder(root);
        postorder(root); 
return 0; 
} 
*/

The above-mentioned approaches are quite similar to each other so if you're able to understand and implement one of them, then others will automatically become easy to understand and implement.
So for 3 different traversals, you don't need to understand 3 different approaches instead you can understand one of the above-mentioned algorithms and the rest will be similar with few minor changes.

Happy Coding!

Comments

Most Popular Posts

100+ STL Algorithms, "I Bet If You Know Even 20 Out of These..." ( C++ 17 )

So, In the previous posts, I've been telling you about the various containers in STL and their implementation. I left a few of them purposely such as unordered_map, unordred_set, etc. so that you also do some homework. This post will entirely be about various functions available in STL and it would be your task to completely code & implement them. Do tell me if you have any doubts related to it in the comment section below. Some common Data Structures Implementation In STL : Stack Syntax : stack<data_type> stack_name Example  : stack<int> st; Built-in functions such as top() ,  push() , pop() , empty() , size() etc. are available in STL for Stack. Queue Syntax :  queue<data_type> queue_name Example  :  queue<int> q; Built-in functions such as push() , pop() , empty() , size() , front() , back() are available in STL for Queue. List ( Doubly Linked List ) Syntax :  list<data_type> list_name Exampl...

Zig-Zag & Spiral Traversal Of A Binary Tree Made Easy

Zig-Zag and Spiral Traversal of a Binary Tree are some of those problems which appear different but have a lot of things in common and thus, can also be solved easily using a common approach. Let's understand what a Zig-Zag & Spiral traversal exactly is : Zig-Zag traversal for the above binary tree is :                                                                                1                                                                            3      2             ...

On Which Coding Platform Should I Practice?

Which is the best online platform to practice, how to improve my coding skills? This post is all about giving answers to all such questions. If you're a beginner who has got no idea what programming is then I would recommend you step by step practice any programming language on Hackerrank. Hackerrank has modules that are self-explanatory and would slowly make you understand all the terminologies which are generally used and with practice, you'll be better at it. Moreover, if you're still not able to understand anything you can always search for your answers on GeekForGeeks or HackerEarth both of them provide one of the best tutorials on any topic. If you're an intermediate who's comfortable with any programming language then you can always improve your problem-solving skills studying Data Structures and Algorithms and practicing it. You can also improve by competitive coding. The best place to start with competitive programming according to me would be ...