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

2011-10-22 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 mohak...@gmail.com wrote: If we want to compute the sum of every column, we can use the following

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

2011-10-22 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 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

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

2011-10-19 Thread monish001
I think it might done using function of following prototype: void func(node* root, dequeint d, const dequeint::iterator it); I will add left child's value in it-1 if exists else create new... similarly for right child. and call the same function for each of the children to explore further..

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 monish.gup...@gmail.com wrote: I think it might done using function of following

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 sravanreddy...@gmail.comwrote: 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

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 rahul.deshmukhpa...@gmail.comwrote: 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) {

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

2011-10-17 Thread Navneet
I have an alternate method. At every level, maintain records with (x-axis value, nodeValue) and keep them in a queue. After traversal is done, you can sort the elements based on x-Axis value and sum up the nodeValue for records having same x-axis value. But complexity is becoming O(nlogn)

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

2011-10-17 Thread sravanreddy001
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 received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit