#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right,*left;
}*root=NULL;
int max=0;
struct node *newnode(int k){
struct node
*temp
=(struct node
*)malloc(sizeof(struct node
));
temp->data=k;
temp->right=temp->left=NULL;
return temp;
}
void height(struct node *root,int d){
if(root==NULL){
return;
}
printf("Node:%d,Height: %d\n",root
->data
,d
);
if(d>max){
max=d;
}
height(root->left,++d);
//here actually ++d and then --d thats why i have used d in next step
// --d to go up ++d to go right
height(root->right,d);
--d;
}
int main(){
root = newnode(1);
root->left = newnode(5);
root->right = newnode(8);
root->left->left = newnode(2);
root->left->right = newnode(4);
root->right->left = newnode(9);
root->right->right = newnode(10);
root->right->right->right= newnode(13);
height(root,0);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCiNpbmNsdWRlPHN0ZGxpYi5oPgoKc3RydWN0IG5vZGV7CgogICAgaW50IGRhdGE7CgogICAgc3RydWN0IG5vZGUgKnJpZ2h0LCpsZWZ0OwoKfSpyb290PU5VTEw7CgppbnQgbWF4PTA7CgpzdHJ1Y3Qgbm9kZSAqbmV3bm9kZShpbnQgayl7CgogICAgc3RydWN0IG5vZGUgKnRlbXA9KHN0cnVjdCBub2RlICopbWFsbG9jKHNpemVvZihzdHJ1Y3Qgbm9kZSkpOwoKICAgIHRlbXAtPmRhdGE9azsKCiAgICB0ZW1wLT5yaWdodD10ZW1wLT5sZWZ0PU5VTEw7CgogICAgcmV0dXJuIHRlbXA7Cgp9Cgp2b2lkIGhlaWdodChzdHJ1Y3Qgbm9kZSAqcm9vdCxpbnQgZCl7CgogICAgaWYocm9vdD09TlVMTCl7CgogICAgICAgIHJldHVybjsKCiAgICB9CgogICAgcHJpbnRmKCJOb2RlOiVkLEhlaWdodDogJWRcbiIscm9vdC0+ZGF0YSxkKTsKCiAgICBpZihkPm1heCl7CgogICAgICAgIG1heD1kOwoKICAgIH0KCiAgICBoZWlnaHQocm9vdC0+bGVmdCwrK2QpOwoKICAgIC8vaGVyZSBhY3R1YWxseSArK2QgYW5kIHRoZW4gLS1kIHRoYXRzIHdoeSBpIGhhdmUgdXNlZCBkIGluIG5leHQgc3RlcAoKICAgIC8vIC0tZCB0byBnbyB1cCArK2QgdG8gZ28gcmlnaHQKCiAgICBoZWlnaHQocm9vdC0+cmlnaHQsZCk7CgogICAgLS1kOwoKICAgICAKCiAgICAgCgogICAgIAoKfQoKaW50IG1haW4oKXsKCiAgICByb290ID0gbmV3bm9kZSgxKTsKCiAgICByb290LT5sZWZ0ID0gbmV3bm9kZSg1KTsKCiAgICByb290LT5yaWdodCA9IG5ld25vZGUoOCk7CgogICAgcm9vdC0+bGVmdC0+bGVmdCA9IG5ld25vZGUoMik7CgogICAgcm9vdC0+bGVmdC0+cmlnaHQgPSBuZXdub2RlKDQpOwoKICAgIHJvb3QtPnJpZ2h0LT5sZWZ0ID0gbmV3bm9kZSg5KTsKCiAgICByb290LT5yaWdodC0+cmlnaHQgPSBuZXdub2RlKDEwKTsKCiAgICByb290LT5yaWdodC0+cmlnaHQtPnJpZ2h0PSBuZXdub2RlKDEzKTsKCiAgCgogICAgaGVpZ2h0KHJvb3QsMCk7CgogICAgcHJpbnRmKCJcbkhlaWdodDogJWQiLG1heCk7CgogICAgcmV0dXJuIDA7CgoKfQ==
Node:1,Height: 0
Node:5,Height: 1
Node:2,Height: 2
Node:4,Height: 2
Node:8,Height: 1
Node:9,Height: 2
Node:10,Height: 2
Node:13,Height: 3
Height: 3