题目:

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 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.

分析:

给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

从根节点判断是否存在一条到叶子节点的路径和等于目标和,等同于判断根节点的左右节点到叶子节点的路径和等于目标和减去根节点的值。

从给的例子中来看,判断根节点5是否有路径和等于22,相当于判断根节点的左节点4是否有路径和等于22减去5,也就是17,继续计算下去,11的时候判断是否有路径和等于13,其左叶子节点等于7,不等于13-11,而右叶子节点为2,等于13-11,返回true。

程序:

/**
* 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 hasPathSum(TreeNode* root, int sum) {
if(root == nullptr) return false;
if(root->left == nullptr && root->right == nullptr && root->val == sum)
return true;
return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
}
};

LeetCode 112. Path Sum路径总和 (C++)的更多相关文章

  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 ...

  2. 112 Path Sum 路径总和

    给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22,              5             / \  ...

  3. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  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 ...

  5. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  6. [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 ...

  7. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. linux 基本命令 1

      Linux基本命令(一) 目标 熟练使用 Linux常用的命令 ls  查看文件 clear   清空 cd pwd mkdir touch rm cp mv tree chmod find gr ...

  2. Unity Glossary

    https://docs.unity3d.com/2018.4/Documentation/Manual/Glossary.html 2D terms 2D Physics terms AI term ...

  3. 下载代码的时候 SSH与http的区别

    SSH: git@gitlab.alibaba-inc.com:damai-mz/mz-market.git HTTP:http://gitlab.alibaba-inc.com/damai-mz/m ...

  4. Python连载23-file_analysis

    一.文件 1.定义:长久保存信息的一种信息集合 2.常用操作:(1)打开关闭(2)读写内容(3)查找 3.open函数 (1)意义:打开文件,带有很多参数 (2)第一个参数:必须有,文件的路径和名称 ...

  5. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  6. php explode容易犯的错误

    php explode容易犯的错误 <pre> $pos = strpos($v, 'Controller'); if (is_numeric($pos)) { $kongzhiqifeg ...

  7. [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

    1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...

  8. ASP.NET web.config 配置里部分参数详细说明

    Session配置 <!-- <identity impersonate = "false" [true|false] userName = "" ...

  9. Weblogic-SSRF漏洞复现

    Weblogic-SSRF漏洞复现 一.SSRF概念 服务端请求伪造(Server-Side Request Forgery),是一种有攻击者构造形成有服务端发起请求的一个安全漏洞.一般情况下,SSR ...

  10. java架构之路-(tomcat网络模型)简单聊聊tomcat(二)

    上节课我们说到的Tomcat,并且给予了一般的tomcat配置,和配置的作用,提到了HTTP/1.1 也就是我们的网络通讯模型,那么HTTP/1.1又代表什么呢.我们来简答看一下. tomcat有四种 ...