-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-th node in the Postorder traversal .cpp
95 lines (77 loc) · 2.04 KB
/
N-th node in the Postorder traversal .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
***************************************************************************************
Q5.Given a Binary tree and a number N, write a program to find the N-th node in the
Postorder traversal of the given Binary tree.
NOTE:-Here I have entered a tree user need not to enter that only for the sack of simplicity.
***************************************************************************************
*/
#include<iostream>
using namespace std;
struct node{
int data;
node * left,*right;
};
//function declarations
node* create_node(int data);
void node_post(node *root,int n);
int main()
{
int N;
node *root=NULL,*temp=NULL;
temp=create_node(1);
//first element in tree
root=temp;
temp=create_node(2);
root->left=temp;
temp=create_node(5);
root->left->right=temp;
temp=create_node(4);
root->left->left=temp;
temp=create_node(9);
root->left->left->right=temp;
temp=create_node(8);
root->left->left->left=temp;
temp=create_node(3);
root->right=temp;
temp=create_node(6);
root->right->left=temp;
temp=create_node(7);
root->right->right=temp;
/*
prentered tree looks like this
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
*/
cout<<"\nWhich node you would like to print in postorder traversal of given tree::";
cin>>N;
node_post(root,N);
return 0;
}
//function to create a node
node* create_node(int data)
{
node *p=new node;
p->data=data;
p->left=p->right=NULL;
return p;
}
//function definition to find N-th node element in post-order traversal of binary-tree
void node_post(node *root,int n)
{
static int f=0;
if(root==NULL)
return ;
if(f<=n)
{
node_post(root->left,n);
node_post(root->right,n);
f++;
if(n==f)
cout<<"Element is::"<< root->data;
}
}