Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Created August 24, 2020 20:43
Show Gist options
  • Save IngeFrodo/6493233d6afe5da3422452c2223daaf1 to your computer and use it in GitHub Desktop.
Save IngeFrodo/6493233d6afe5da3422452c2223daaf1 to your computer and use it in GitHub Desktop.
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