Pre-condition:
typedef struct intnode {
int info;
node *left, *right;
}node;
Algorithm:step 1: if pointer is null, return;
step 2: visit left-sub tree, repeat step 1
step 3: visit right-sub-tree, repeat step 1
step 4: visit root
step 5: stop
Code:
void postorder(node *p){
// if pointer is null, return
if( p == NULL )
return;
// visit left
postorder( p->left );
// visit right
postorder( p->right );
// visit root
printf("%d ", p->info );
return;
}
Complexity:O(n), because each node has to be visited atleast once and increases linearly for increasing 'n'.
No comments :
Post a Comment