-
-
Save InterviewBytes/7a7dfd7996151846ccb0292f4de7e557 to your computer and use it in GitHub Desktop.
Sum of left leaves
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
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; | |
} | |
} |
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
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