Hey everyone. I've built binary tree using recursion and included a function to print out the sum of the level of the tree of a given input by a user. My problem isn't that my function doesn't work, but it prints out to the console each levels sum where I only want the level given from the input. For example, if this is my tree:
/ \ / \
4 5 6 7
it will print out:
where I only want it to print out 22.
Now I see in my function why is it printing out all lines, but can't figure out a different solution. I'm using a static variable to add the total up. If you suggest using a global static variable, I already tried and doesn't work. Any suggestions would really help. The function code is below and based on the tree above as a sample tree with the input -3. Thanks a ton!
Well, I actually already fixed the problem. I passed in, by reference, a variable "sum" and set it to 0, then after the function call, cout sum on the next line. Worked perfectly. Thanks though, that would work to.