给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。
叶子节点 是指没有子节点的节点。
链接:https://leetcode-cn.com/problems/path-sum
/**
* 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) return false
let res = false
const depthOrder = (root, sum) => {
if(!root.left && !root.right && sum === targetSum){
res = true
}
if(root.left) depthOrder(root.left, sum+root.left.val)
if(root.right) depthOrder(root.right, sum+root.right.val)
}
depthOrder(root, root.val)
return res
};