Created
August 24, 2020 20:43
-
-
Save IngeFrodo/6493233d6afe5da3422452c2223daaf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SumOfLeftLeaves { | |
private int leavesSum; | |
public int sumOfLeftLeaves(TreeNode root) { | |
leavesSum = 0; | |
dfs(root, false); | |
return leavesSum; | |
} | |
private void dfs(TreeNode node, boolean left) { | |
if (node == null) { | |
return; | |
} | |
if (node.left == null && node.right == null && left) { | |
leavesSum += node.val; | |
} else { | |
dfs(node.left, true); | |
dfs(node.right, false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment