let array arr[n] is used to store the vertical sum for "n" vertical lines
n= number of nodes till the extreme left from root + number of nodes till
the extreme right from root
in this case ..it is 3 + 3
now pass the array to this function with value of i=number of nodes till
the extreme left from root.(in this case it is 3) & assuming array is
starting from base 1 to n.
void sumVertical(int arr[],node *ptr,int i)
{
if(!ptr)
return;
arr[i]+=ptr->data;
sumVertical(arr,ptr->left,i-1);
sumVertical(arr,ptr->right,i+1);
}
correct me if am wrong..
On Thu, Jan 19, 2012 at 8:41 PM, Coding Geek <[email protected]> wrote:
> Given a binary tree with no size limitation, write a program to find the
> sum of each vertical level and store the result in an appropriate
> data structure (Note: You cannot use an array as the tree can be of any
> size).
>
> 4
> / \
> 7 8
> / \ / \
> 10 11 / 13
> 12
>
> here 4(root) , 11(leftsubtree's right child ), 12 (rightsubtree's
> left child) are in same vertical Line
>
>
> so here vertical line 1 is from 10
> vertical line 2 sum is 7
>
> vertical line 3 sum is 4+11+12=27 (May Have Some Doubt So i Have
> represented the figure in correct way)
>
> vertical line 4 is 8
> vertical line 5 is 13
>
>
>
>
>
> --
>
> "To Iterate is Human, To Recurse is Divine"
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
*Dheeraj Sharma*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.