cosmos icon indicating copy to clipboard operation
cosmos copied to clipboard

Sum of left leaves in Binary Tree

Open nimit95 opened this issue 9 years ago • 11 comments

I would like to implement it in C++

nimit95 avatar Oct 06 '17 14:10 nimit95

Sure, Go for it 👍

Be sure to check existing implementations 👍

AdiChat avatar Oct 06 '17 14:10 AdiChat

Hi! I like this project a lot and I would really like to help :D. Not sure how is the implementation policy here tho. Should I just write a .cpp with a function sum(int tree[]) in it and make a PR with it? Also, I didn't quite get what your meaning of "left leaves" mean. A leaf in a binary tree is a node with no children.So should I just take the sum of the first half of them? Or did you mean the sum of left children?

PinkiePie1189 avatar Oct 06 '17 14:10 PinkiePie1189

@PinkiePie1189 Glad you liked it 😍 You can make this great.

There is room for every style of implementation, just make sure there are no duplicate files. Browse through our code base and submit a pull request for any problem which you think should belong here 👍

You can take a look at our Contribution guidelines as well 👍

AdiChat avatar Oct 06 '17 14:10 AdiChat

I will do this in C

rstalekar avatar Oct 06 '17 15:10 rstalekar

Yes , I too didn't understand this , does this mean we need to sum all the leaves of the left subtree from the root or is it sum of the left leaves of the left subtree from the root. Please clarify.

rstalekar avatar Oct 06 '17 16:10 rstalekar

In my opinion, he meant the sum of all leaves having the property of being the left child of its parent node 👍

AdiChat avatar Oct 06 '17 16:10 AdiChat

@AdiChat if that's the case,doesn't that mean that we only need to all sum even numbered nodes,which makes the implentation trival?

PinkiePie1189 avatar Oct 06 '17 16:10 PinkiePie1189

@PinkiePie1189 Yes, you are right 👍

In array representation (root at index 1), the solution will be the sum of all even numbered nodes. In pointer representation, we need to traverse the tree and check if the left child is a leaf or not.

In both cases, the implementation is trivial 😄

AdiChat avatar Oct 06 '17 16:10 AdiChat

@PinkiePie1189 you are right but the array representation would take extra memory in the binary tree.

nimit95 avatar Oct 06 '17 17:10 nimit95

Ok I am implementing in C.

rstalekar avatar Oct 06 '17 17:10 rstalekar

I have created pull request, please review.

rstalekar avatar Oct 06 '17 18:10 rstalekar