leecode/backlog/112.path-sum.js
2020-05-22 18:17:19 +08:00

30 lines
589 B
JavaScript

/*
* @lc app=leetcode id=112 lang=javascript
*
* [112] Path Sum
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} sum
* @return {boolean}
*/
var hasPathSum = function(root, sum) {
// check
if (root === null) return false;
// if it's leaf:
if (root.left === null && root.right === null) {
return root.val === sum;
}
return (
hasPathSum(root.left, sum - root.val) ||
hasPathSum(root.right, sum - root.val)
);
};