Skip to main content

Posts

Showing posts with the label inorder

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 ;   ...