要求

  • 给出一个二叉树及数字sum,判断是否存在一条从根到叶子的路径,路径上的所有节点和为sum

实现

  • 转化为寻找左右子树上和为 sum-root 的路径,到达叶子节点时递归终止
  • 注意只有一个孩子时,根节点本身不构成一条路径,如下图sum=5的情况,终止条件是不对的
 1 class Solution {
2 public:
3 bool hasPathSum(TreeNode* root, int sum) {
4
5 if( root == NULL )
6 return false;
7
8 if( root->left == NULL && root->right == NULL )
9 return root->val == sum;
10
11 if( hasPathSum( root->left , sum - root->val ) )
12 return true;
13
14 if( hasPathSum( root->right , sum - root->val ) )
15 return true;
16
17 return false;
18 }
19 };

    

相关

  • 111 Minimum Depth of Binary Tree
  • 404 Sum of Left Leaves

[刷题] 112 Path Sum的更多相关文章

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

  2. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  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 ☆(二叉树是否有一条路径的sum等于给定的数)

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

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

  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]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

随机推荐

  1. 极速精简 Go 版 Logstash

    前言 今天来介绍 go-zero 生态的另一个组件 go-stash.这是一个 logstash 的 Go 语言替代版,我们用 go-stash 相比原先的 logstash 节省了2/3的服务器资源 ...

  2. 第1课:Linux操作系统基础【DevOps基础培训】

    第1课:Linux操作系统基础 --DevOps基础培训 1. 云主机.公网IP 1.1 公网ip和私网ip 只有公网ip是能够连接互联网的,私网IP 一般只用作局域网 我们能够上网靠的是isp组织分 ...

  3. 第30 章 : 理解 RuntimeClass 与使用多容器运行时

    理解 RuntimeClass 与使用多容器运行时 本文将主要分享以下三方面的内容: RuntimeClass 需求来源 RuntimeClass 功能介绍 多容器运行时示例 RuntimeClass ...

  4. 设计原则:单一职责(SRP)原则

    1 什么是单一职责(SRP)原则 单一职责原则的英文是 Single Responsibility Principle,缩写为 SRP.翻译过来就是:一个类或者模块只负责完成一个职责(或者功能). 所 ...

  5. Python基础(七):字符串的使用(上)

    什么是字符串? 字符串该如何定义呢?通俗的说:字符串就是一系列的字符. 创建字符串的3种方式 单引号创建 >>> a = 'I am a student' >>> ...

  6. 从wav到Ogg Opus 以及使用java解码OPUS

    PCM 自然界中的声音非常复杂,波形极其复杂,通常我们采用的是脉冲代码调制编码,即PCM编码.PCM通过抽样.量化.编码三个步骤将连续变化的模拟信号转换为数字编码. 采样率 采样频率,也称为采样速度或 ...

  7. Kubernetes网络概念初探

    ------------恢复内容开始------------ Kubernetes网络是Kubernetes中一个核心概念.简而言之,Kubernetes网络模型可以确保集群上所有Kubernetes ...

  8. 后续来啦:Winform/WPF中快速搭建日志面板

    后续来啦:Winform/WPF中快速搭建日志面板 继昨天发文ASP.NET Core 可视化日志组件使用(阅读文章,查看视频)后,视频下有朋友留言 "Winform客户端的程序能用它不?& ...

  9. 1.稀疏数组_java实现

    稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素行列及值记录在一个小规 ...

  10. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作centos6.5-14

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作centos6.5-14 欢迎加QQ群:1026880196 进行交流学习 制作OpenSta ...