One line solution in Java using recursion. Runtime 1 ms


  • -8
    V
    public boolean hasPathSum(TreeNode root, int sum) {
                   return (root == null) ? false : (root.left == null && root.right == null) ? sum - root.val == 0 : (hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val));
                }

  • 0
    H

    one line definitely, but not short, nor clean


  • 0
    A

    One line that you'll neve want to go back to. This is ugly, man.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.