Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.
Code:
void reverseAlternateLevelOfPrefectBTUtil(node* left,node* right,int level,int d){
if(left==NULL || right==NULL) return;
if(level%2!=0 && level==d){
int a= left->data;
int b= right->data;
swap(&a,&b);
left->data = a;
right->data = b;
}
reverseAlternateLevelOfPrefectBTUtil(left->left,right->right,level+1,d);
reverseAlternateLevelOfPrefectBTUtil(left->right,right->left,level+1,d);
}
void reverseAlternateLevelOfPrefectBT(node* root)
{
for(int i=1; i<=height(root); i+=2)
reverseAlternateLevelOfPrefectBTUtil(root->left,root->right,1,i);
cout<<"Level Order After modification\n";
levelorder(root);
}
No comments:
Post a Comment