#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 reverse_preorder(TreeNode *root)
{
// base case
if (root == NULL)
return;
// First traverse current node
printf("%d ", root->val);
// secondly traverse right sub tree
reverse_preorder(root->right);
// Finally traverse left sub tree
reverse_preorder(root->left);
}
int main()
{
// building the tree
TreeNode *t = new TreeNode(7);
t->left = new TreeNode(1);
t->left->left = new TreeNode(11);
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("Reverse preorder traversal tree is:\n");
reverse_preorder(t);
return 0;
}
Output
Reverse preorder traversal tree is:
7 9 10 8 1 3 5 6 4 2 11
0 Comments