Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 11, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/7a7dfd7996151846ccb0292f4de7e557 to your computer and use it in GitHub Desktop.
Save InterviewBytes/7a7dfd7996151846ccb0292f4de7e557 to your computer and use it in GitHub Desktop.
Sum of left leaves
package com.interviewbytes.trees;
public class SumOfLeftLeaves {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int sum = 0;
if (root.left != null && root.left.left == null && root.left.right == null) sum = sum + root.left.val;
sum = sum + sumOfLeftLeaves(root.left);
sum = sum + sumOfLeftLeaves(root.right);
return sum;
}
}
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment