15,990,892 members
1.00/5 (1 vote)
See more:
Given the root of a binary tree, return the sum of all left leaves.
Please give me a detailed dry run as I am not able to find the error in my code. I know the correct answer, but cannot analyse my code.

What I have tried:

Java
```/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 0;
}
if(root.left.left== null && root.left.right == null){
sum++;
}
int left = sumOfLeftLeaves(root.left);
int right = sumOfLeftLeaves(root.right);
sum = left+ right;
return sum;
}
}```
Posted
Updated 20-Sep-23 3:49am
v2
Richard MacCutchan 20-Sep-23 4:33am
The question states: "return the sum of all left leaves", but that is not what you are calculating.

## Solution 1

Quote:
I know the correct answer but cannot analyse my code.
That's a pity, since you wrote it and thus are the best person to fix it as you understand what it is doing!

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
And the process of "finding an error in code" is called debugging and it's something that every developer has to do, pretty much all the time: it takes longer to ensure code it right and working than it does to write it!

So you need to learn how to do it, and me fixing your code won't do that - you need to do it for yourself in order to develop the skill.

Start with three things: the input (`root` in this case), the expected answer (you know this, you said so), and the actual answer (run your code and find out).
Check the input: is it exactly what you expected it to be? Use the debugger (a quick google for the name of your IDE and "debugger" will get you instructions on how to use it) and actually look at the tree structure your method has been passed: write it down on paper to make sure it really is what you think it should be. If it isn't, then there is no point in checking the method itself as the result won't be what you want.

When you have valid input, look at the expected value and the actual. How are they different? is it "off by one"? An order of magnitude out? Zero? Think about the tree and consider what about it might have generated that result - for example, is it counting all nodes instead of just the left ones?
Working out possible routes to that erroneous result can give you a lot of clues about what is happening in your code that shouldn't, or not ha-ppenign that should.

We you've done that continue to use the debugger to single step though your code while it is running and work out what you expect to happen for each line before you execute it. Did it happen as you wanted? If so, keep going. If not, why not?

For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```private int Double(int value)
{
return value * value;
}```

And a quick bit of debugging will show you exactly what is wrong!

Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

## Solution 2

Quote:
I am unable to find error in this code
Probably time to learn debugger.
Why are you killing the previous value of `sum` in this line ?
C++
`sum = left+ right;`

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Unit testing is also a good idea.
Unit testing - Wikipedia[^]

## Solution 3

Using a recursive method means you need to add the result of calling the method to your current value. So if `root` or `root.left` are `null` you return `zero`. If `root.left.left` is `null`, then you return `1`, as there is only a single leaf. But if `root.left.left` is not `null`, then you need to return `1` plus the return value of calling `sumOfLeftLeaves(root.left)`. This way, the total count should correctly accumulate.

v2