#include <bits/stdc++.h>
using namespace std;
class TreeNode
{ // tree node is defined
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
val = data;
left = NULL;
right = NULL;
}
};
void inorder(TreeNode *root)
{
// base case
if (root == NULL)
return;
// traverse left sub tree
inorder(root->left);
// traverse current node secondly
printf("%d ", root->val);
// traverse right sub tree finally
inorder(root->right);
}
int main()
{
// building the tree
TreeNode *t = new TreeNode(7);
t->left = new TreeNode(1);
t->left->left = new TreeNode(0);
t->left->right = new TreeNode(3);
t->left->right->left = new TreeNode(2);
t->left->right->right = new TreeNode(5);
t->left->right->right->left = new TreeNode(4);
t->left->right->right->right = new TreeNode(6);
t->right = new TreeNode(9);
t->right->left = new TreeNode(8);
t->right->right = new TreeNode(10);
printf("Inorder traversal of the above tree is:\n");
inorder(t);
return 0;
}
Output
Inorder traversal of the above tree is:
0 1 2 3 4 5 6 7 8 9 10
0 Comments