GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

diagonal traversal of tree -->(is the code cortect??)....plz check....?

#include<bits/stdc++.h>
using namespace std;

struct node *insert(int);
void diagonalTrav(struct node*);

struct node{
int data;
struct node *left,*right;
};

struct node *insert(int key){
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=key;
temp->left=NULL;
temp->right=NULL;
return temp;
}

void diagonalTrav(struct node *root){
if(!root){
cout<<"empty tree";
return;
}
struct node *p=root;
queue <struct node*=""> s;
while(p){
cout<<p->data<<" ";
if(p->right){
s.push(p->right);
}
p=p->left;
}
while(!s.empty()){
p=s.front();
if(p->right){
s.push(p->right);
}
cout<<p->data<<" ";
while(1){
if(p->left){
cout<<p->left->data<<" ";
if(p->left->right){
s.push(p->left->right);
}
}
else{
break;
}
p=p->left;
}
s.pop();
}
}

int main(){
struct node *root=NULL;
root=insert(1);
root->left=insert(2);
root->right=insert(3);
root->left->left=insert(4);
root->left->right=insert(5);
root->right->left=insert(6);
root->right->right=insert(7);
root->left->left->left=insert(8);
root->left->right->left=insert(9);
root->left->right->left->right=insert(12);
root->right->right->left=insert(10);
root->right->right->right=insert(11);
root->right->right->right->right=insert(14);
root->right->right->right->left=insert(13);

diagonalTrav(root);

return 0;
}

asked Oct 19, 2016 by shell_007
...