题目:

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. Nginx 负载均衡算法

    Nginx 负载均衡算法 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务,如果后端某台服务器死机,自动剔除故障系统,使用户访问不受影响. upstream tomcat_server { ...

  2. Loadrunner 进行压力测试 并发测试

    问题背景: 今年公司项目进行Saas化转型,在中间遇到很多问题,其中之一就是 Saas化后多租户的性能 和 并发问题.公司让我来调研和重现问题,通过调研总结了一些经验教训,分享给大家. 环境: Loa ...

  3. 周董新歌搞崩QQ,抓取20W评论看看歌迷在说啥

    9 月 16 日晚间,周董在朋友圈发布了最新单曲<说好不哭> 发布后,真的让一波人哭了 一群想抢鲜听的小伙伴直接泪奔 因为 QQ 音乐直接被搞崩了 没想到干翻 QQ 音乐的不是网易云音乐 ...

  4. 中秋快乐,分享福利脑图:入门spring cloud

  5. python命令行解析函数

    sys.argv 在终端运行python 1.py hahah import sys print(sys.argv) # ['1.py', 'hahah'] argparse Python的命令行解析 ...

  6. Wireshark使用入门

    目录 1. Wireshark介绍 1.1 客户端界面 1.2 Display Filter 的常用方法 1.3 界面上一些小TIPS 2. 使用Wireshark分析TCP三次握手过程 2.1 三次 ...

  7. Java连载38-对象封装性格式、构造方法

    一.封装的步骤 (1)所有属性私有化,使用private关键字进行修饰,private表示私有的,修饰的所有数据只能在本类中进行访问. (2)对外提供简单的操作入口,也就是说以后外部程序要想访问age ...

  8. css样式重置 移动端适配

    css  默认样式重置 @charset "utf-8"; *{margin:0;padding:0;} img {border:none; display:block;} em, ...

  9. select下拉框option的样式修改

    select原样式: 进行样式修改后的样式: 附上修改代码: //select外面必须包裹一个div,用来覆盖select原有的样式<div class="option"&g ...

  10. 常用的排列、组合、阶乘函数 MATLAB

    1.求n的阶乘,方法如下:a.factorial(n)b.gamma(n+1)c.v='n!'; vpa(v) 2.求组合(数),方法如下:a.combntns(x,m)    列举出从n个元素中取出 ...