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 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 there exist a root-to-leaf path 5->4->11->2 which sum is 22.

分析

假设是一个二叉搜索树,那么能够依据它的特性来做一些减法走一下捷径。

先用最主要的方法来求解试试:

bool hasPathSum(TreeNode* root, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return root->val == sum;
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

然后我在想。假设在某个节点上的和已经超过了sum,那应该就false了吧。

然后加了一行:

if (root->val > sum) return false;      

噢不……原来还能够有负数的,错了。算了不指望这个了,继续改着玩……

事实上这个和上面的几乎相同。区别在于上面的推断方法是不断的用给定的sum减掉当前的值,而这个是不断往上累加看是否能累计到刚好等于sum,还引入了额外的变量……

bool dfs(TreeNode* root, int pasum, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return pasum + root->val == sum;
    return dfs(root->left, pasum + root->val, sum) || dfs(root->right, pasum+root->val, sum);
}

bool hasPathSum(TreeNode* root, int sum) {
    return dfs(root, 0, sum);
}

代码

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
    bool dfs(TreeNode* root, int pasum, int sum) {
        if (!root) return false;
        if (!root->left && !root->right)   return pasum + root->val == sum;
        return dfs(root->left, pasum + root->val, sum) || dfs(root->right, pasum + root->val, sum);
    }
    bool hasPathSum(TreeNode* root, int sum) {
        return dfs(root, 0, sum);
    }
};
时间: 2024-10-09 10:50:42

LeetCode 112 Path Sum(路径和)(BT、DP)(*)的相关文章

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 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 思路:求解二叉树的路径和是否满足

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

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

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

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 112 Path Sum ----- java

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

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文: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. 中文:给定一颗二叉树,如