记录每日LeetCode 112.路径总和 Java实现

题目描述:

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

初始代码:

/**
 * 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) {

    }
}

示例1:

输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。

示例2:

输入:root = [1,2,3], targetSum = 5
输出:false
解释:树中存在两条根节点到叶子节点的路径:
(1 --> 2): 和为 3
(1 --> 3): 和为 4
不存在 sum = 5 的根节点到叶子节点的路径。

示例3:

输入:root = [], targetSum = 0
输出:false
解释:由于树是空的,所以不存在根节点到叶子节点的路径。

参考答案:

/**
 * 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) {
        if(root == null) return false;
        //这题的做法和层序遍历相同 即广度优先遍历
        Queue<TreeNode> node = new LinkedList<>();//用来存储节点
        Queue<Integer> value = new LinkedList<>();//用来存储每个节点的值
        node.offer(root);//默认将根节点先放入
        value.offer(root.val);//默认将根节点的值先放入
        while(!node.isEmpty()){
            TreeNode n = node.poll();//取出存储的节点
            int v = value.poll();//取出存储的值
            //题目是求根节点到叶子节点的路径 所以需要这样判断
            if (n.left == null && n.right == null) {
                if (v == targetSum) return true;
                continue;
            }
            if(n.left != null){
                node.offer(n.left);
                value.offer(n.left.val + v);
            }
            if(n.right != null){
                node.offer(n.right);
                value.offer(n.right.val + v);
            }
        }
        return false;
    }
}
/**
 * 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) {
        if (root == null) return false;
        //根节点到叶子节点
        if (root.left == null && root.right == null) return targetSum == root.val;
        //遍历左子树或者右子树成功一个即可
        return hasPathSum(root.left, targetSum - root.val) || 
        hasPathSum(root.right, targetSum - root.val);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_65563175/article/details/128706270