leetCode 112.Path Sum (路径和) 解题思路和方法

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:

Given the below binary tree and sum
= 22
,

              5
             /             4   8
           /   /           11  13  4
         /  \              7    2      1

思路:求解二叉树的路径和是否满足sum。需要考虑的一点就是有负数,而且如果为0的时候也不能结束,以为也有可能下面的和为0.

题目不难,只是有一些小的点需要注意。具体代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        /**
         * 判断根节点是否为空和值是否大于sum,是则false
         * 然后判断val和sum是否相等,如果相等且root没有子树。true
         * 不相等则递归判断左右子树是否与sum-val相等
         */
        if(root == null){//注意,此处不能判断val与sum大小,因为存在负数
            return false;
        }
        if(root.val == sum){
            //注意:因为存在负数,所以左右子树不为空也不能直接返回flase
            //有可能左右子树的和值为0
            if(root.left == null && root.right == null)
                return true;
        }
        //小于
        return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 10:50:41

leetCode 112.Path Sum (路径和) 解题思路和方法的相关文章

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

LeetCode 112. Path Sum路径总和 (C++)

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / 4

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

Java [Leetcode 112]Path Sum

题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true

Java for LeetCode 112 Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

LeetCode 112 Path Sum(路径和)(BT、DP)(*)

翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 返回真.由于这里存在一条根叶路径(5->4->11->2),它的和为22. 原文 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha

LeetCode 112. Path Sum (二叉树路径之和)

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

112 Path Sum 路径总和

给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22,              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1返回 true, 因为存在总和为 22 的根到叶的路径 5->4->11->2.详见:https://l