algorithm - Recursion in Binary Tree -


i trying solve problem in tree,and stuck in recursion , write code. cant't figure out why it's giving out ! how code gives output program x=0,y=0

int height1(node *root,int x,int y)  {    int val;      if(root==null)     return 1;     else     {         x=x+height1(root->left,x,y);         y=y+height1(root->right,x,y);         printf("x=%d , y=%d %d\n",x,y,root->data);         if(x>y)             return x;             else                 return y;     } 

it's rough work understand flow of recursion. input of tree traversing 50 40 70 45 30 44 48 , value of

       x  y  root->data        1   1    30        2   1    44       4   1    48       3    4    45       1    4    40       5     1    70       4    5     50 

why going out in way ,in way should add y , x each time value should increase,

please give me advise ,because every time solve tree problem stuck in recursion, can suggest right approach.

i understand figure out base condition important, , know how find.but can't how output came.

i know here many experienced programmers , want please give me advice because think faced problems in recursion ,so how solve this.how solve recursive problem when new 1 come.

should refer book or should solve programs. please want advise can become in this.

this code has rather strange architecture in general. leave 2 int arguments away, useless. next problem: code doesn't count nodes except leaves. , code has logical mistakes aswell (example: if(root == null) return 1; lead counting nodes don't exist). in general implemented lot simpler:

int height1(node *root){     if(root == null)         return 0;      int l = height1(root->left);     int r = height1(root->right);      return (l < r ? r : l) + 1; } 

Comments