题目
https://leetcode-cn.com/problems/path-sum/
思路
深度优先遍历
从root遍历到叶子节点,看叶子节点的val是否等于剩余的targetSum
解法
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val = (val===undefined ? 0 : val)* this.left = (left===undefined ? null : left)* this.right = (right===undefined ? null : right)* }*//*** @param {TreeNode} root* @param {number} targetSum* @return {boolean}*/var hasPathSum = function(root, targetSum) {if (root === null) {return false}if (!root.left && !root.right) {return targetSum === root.val}return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val)};
