1 public class Solution { 2 public boolean hasPathSum(TreeNode root, int sum) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return false; 7 boolean[] result = new boolean[1]; 8 traversal(root, sum, result); 9 return result[0]; 10 } 11 12 private void traversal(TreeNode root, int sum, boolean[] result) 13 { 14 if(root.left == null && root.right == null){ 15 if(sum == root.val) 16 result[0] = true;; 17 return;} 18 if(root.left != null) 19 traversal(root.left, sum - root.val, result); 20 if(root.right != null) 21 traversal(root.right, sum - root.val, result); 22 } 23 }