Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-21 Thread ashish shukla
initialize array with 0 and provide appropriate value to c otherwise it will be negative and here c is using as index for array. procedure visit(node root,int s[],int c) { if (root= null) return 0; S[c] += n.value; visit(root->left, c - 1) visit(root->right, c + 1) } On Fri,

Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-21 Thread ashish shukla
procedure visit(node root,int c) { if (root= null) return 0; S[c] += n.value; visit(root->left, c - 1) visit(root->right, c + 1) } On Fri, Oct 21, 2011 at 1:22 PM, Mohak Naik wrote: > If we want to compute the sum of every column, we can use the following > algorithm > > a

Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-21 Thread Mohak Naik
If we want to compute the sum of every column, we can use the following algorithm procedure visit(node n, int c) { if (n.left != null) S{c} += n.value; visit(n.left, c - 1) visit(n.right, c + 1) } and call once visit(A, 0), where A is the root node. Note that S{...} in the algorithm

Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-19 Thread yq Zhang
You should use a doubly linked list instead of any sort of array. All the operation on the data structure you need are goto next/prev and insert front/end. Yunqiao On Wed, Oct 19, 2011 at 6:40 AM, monish001 wrote: > I think it might done using function of following prototype: > void func(node*

Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-18 Thread rahul patil
On Tue, Oct 18, 2011 at 6:31 PM, rahul patil wrote: > Use recursion with the code as follows > > sum array of sums, with horizontal levels, level 0 is for leftmost element > > void add(node *root, int *sum, int level) > { > if(root->left) > { if(level !=

Re: [algogeeks] Re: print vertical sums of a binary tree

2011-10-18 Thread rahul patil
Use recursion with the code as follows void add(node *root, int *sum) { } On Tue, Oct 18, 2011 at 8:58 AM, sravanreddy001 wrote: > I that is what is suggested in the above code. Even here you can avoid the > sorting and add all with same x coordinate.. O(n) space as suggested.. > > -- > You