17-lt-路径最大和

112. 路径总和

思路:

对于二叉树的一条合适路径的选择,直接使用DFS,但是这道题在调试的时候还是遇到了小麻烦,

注意点:

1. base case 的中断条件是 左右子节点都为空,叶子结点返回

2. 值进行和目标值对比

遇到的难点:

没分析清楚叶子节点 ,直接 root == null进行了比较;叶子结点是重要点

结果:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        //DFS 直接深度搜索遍历
        if(root == null){
            return false;
        }
        return DFS(root,targetSum,0);

    }

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

其他题解:

1.利用 sum-root.val 最后求得结果相等,有点那个从链表中找是否存在满足target的值的思路;利用的也是 target - value ;DFS就是递归

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

2.BFS

两个队列同步表示此位置的值的和为多少

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> queNode = new LinkedList<TreeNode>();
        Queue<Integer> queVal = new LinkedList<Integer>();
        queNode.offer(root);
        queVal.offer(root.val);
        while (!queNode.isEmpty()) {
            TreeNode now = queNode.poll();
            int temp = queVal.poll();
            if (now.left == null && now.right == null) {
                if (temp == sum) {
                    return true;
                }
                continue;
            }
            if (now.left != null) {
                queNode.offer(now.left);
                queVal.offer(now.left.val + temp);
            }
            if (now.right != null) {
                queNode.offer(now.right);
                queVal.offer(now.right.val + temp);
            }
        }
        return false;
    }
}

总结:

一点一点积累,总会会的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带着希望活下去

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值