Re: [algogeeks] print vertical sums of a binary tree
@Ankur NO Need of Two Array , Only One will be Suffice :) Just Initialized array with 0 for all nodes initiallly Thanks Shashank CSE,BIT Mesra -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/OzVLE9O8fokJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] print vertical sums of a binary tree
I am assuming we are allowed to use space ..With that i make 2 arrays 1 to store values from left side and other from right side . Here is the func void getLevelSum(node* root,int minL,int maxR,int level){ if(!root) return ; minL = min(minL,level); maxR = max(maxR,level); getLevelSum(root-left,minL,maxR,level-1); getLevelSum(root-right,minL,maxR,level+1); } void getLevelSum(node* root,int level,int minL,int maxR,int left[],int right[]){ if(!root) return ; minL = min(minL,level); maxR = max(maxR,level); getLevelSum(root-left,level-1,minL,maxR,left,right); getLevelSum(root-right,level+1,minL,maxR,left,right); if(level0) left[abs(level)] +=root-data; else right[level] +=root-data; } I am traversing whole tree nodes only once but storing them in an array so Space complexity is also O(n) for me Anyone having a better solution or approach ? On Sun, Oct 16, 2011 at 9:27 AM, SUMANTH M sumanth.n...@gmail.com wrote: Hi, A binary tree is given we need to print vertical sums of nodes. for example 12 34 5 | | 5| | | | / | \ | | | | / |8 | | | / | / |\| | 4 |/| 10 |/ | \9| | | / | \ | | 7 | 6 | | | | | | | | | | | --- 7 4 20 8 10 Here we need to print sum 7,4,20,8,10. -Thanks -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] print vertical sums of a binary tree
Sorry code got reposted twice here is the correct one void getLevelSum(node* root,int level,int minL,int maxR,int left[],int right[]){ if(!root) return ; minL = min(minL,level); maxR = max(maxR,level); getLevelSum(root-left,level-1,minL,maxR,left,right); getLevelSum(root-right,level+1,minL,maxR,left,right); if(level0) left[abs(level)] +=root-data; else right[level] +=root-data; } On Sun, Oct 16, 2011 at 4:23 PM, Ankur Garg ankurga...@gmail.com wrote: I am assuming we are allowed to use space ..With that i make 2 arrays 1 to store values from left side and other from right side . Here is the func void getLevelSum(node* root,int level,int minL,int maxR,int left[],int right[]){ if(!root) return ; minL = min(minL,level); maxR = max(maxR,level); getLevelSum(root-left,level-1,minL,maxR,left,right); getLevelSum(root-right,level+1,minL,maxR,left,right); if(level0) left[abs(level)] +=root-data; else right[level] +=root-data; } I am traversing whole tree nodes only once but storing them in an array so Space complexity is also O(n) for me Anyone having a better solution or approach ? On Sun, Oct 16, 2011 at 9:27 AM, SUMANTH M sumanth.n...@gmail.com wrote: Hi, A binary tree is given we need to print vertical sums of nodes. for example 12 34 5 | | 5| | | | / | \ | | | | / |8 | | | / | / |\| | 4 |/| 10 |/ | \9| | | / | \ | | 7 | 6 | | | | | | | | | | | --- 7 4 20 8 10 Here we need to print sum 7,4,20,8,10. -Thanks -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] print vertical sums of a binary tree
Even I see this as the best solution... O(n) time and space.. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/sIzIL2amUckJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] print vertical sums of a binary tree
Hi, A binary tree is given we need to print vertical sums of nodes. for example 12 34 5 | | 5| | | | / | \ | | | | / |8 | | | / | / |\| | 4 |/| 10 |/ | \9| | | / | \ | | 7 | 6 | | | | | | | | | | | --- 7 4 20 8 10 Here we need to print sum 7,4,20,8,10. -Thanks -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.