Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. private int leftsum = 0;
  12. public int sumOfLeftLeaves(TreeNode root) {
  13. if(root==null) return 0;
  14.  
  15. helper(root,0);
  16. return leftsum;
  17. }
  18.  
  19. public void helper(TreeNode node, int k){
  20. if(k==1 && node.left==null && node.right==null)
  21. leftsum+=node.val;
  22.  
  23. if(node.left!=null){
  24. helper(node.left,1);
  25. }
  26.  
  27. if(node.right!=null){
  28. helper(node.right,0);
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement