[LeetCode]Path Sum系列

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode]Path Sum系列相关的知识,希望对你有一定的参考价值。

1.二叉树路径求指定和,需要注意的是由于有负数,所以即使发现大于目标值也不能返回false,而且返回true的条件有两个,到叶节点且等于sum,缺一不可

public boolean hasPathSum(TreeNode root, int sum) {
        if (root==null) return false;
        if (root.val==sum&&root.left==null&&root.right==null) return true;
        else return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum- root.val);
    }

2.

3.

4.

以上是关于[LeetCode]Path Sum系列的主要内容,如果未能解决你的问题,请参考以下文章

一天一道LeetCode#113. Path Sum II

leetcode:Minimum Path Sum

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

[LeetCode] 113. Path Sum II

[LeetCode]题解(python):112 Path Sum

[LeetCode]题解(python):113 Path Sum II