Advertisement
yimengael

Root to leaf path sum equal to K

Mar 5th, 2022
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1.     static Boolean state = false;
  2.    
  3.     static Boolean path_sum(BinaryTreeNode root, Integer k) {
  4.         path_sum_helper(root, k);
  5.         return state;
  6.     }
  7.    
  8.     static void path_sum_helper(BinaryTreeNode root, Integer target) {
  9.         if (root.left == null && root.right == null) {
  10.             if (target.equals(root.value)) {
  11.                 state = true;
  12.                 return;
  13.             }
  14.         }
  15.        
  16.         if (root.left != null) {
  17.             path_sum_helper(root.left, target - root.value);
  18.         }
  19.        
  20.         if (state == true) {
  21.             return;
  22.         }
  23.        
  24.         if (root.right != null) {
  25.             path_sum_helper(root.right, target - root.value);
  26.         }
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement