要求

  • 给出一个二叉树及数字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. 用pyqt5做一个简易的音乐播放器

    需求 要求可以读取音频文档,有播放和暂停的功能 附上代码(1)UI界面 # -*- coding: utf-8 -*- # Form implementation generated from rea ...

  2. 围绕 Kubernetes 的 8 大 DevOps 生产关键实践

    本文主要介绍 DevOps 的 8 大关键实践在 Kubernetes 平台下如何落地,结合我们目前基于 Kubernetes 平台的 DevOps 实践谈谈是如何贯彻相关理念的,这里不会对其具体实现 ...

  3. Netflix业务运维分析和总结

    目录 Netflix工作环境的分析和思考 为什么Netflix会做得如此极致? 海量业务规模下的技术架构和挑战 更加合理的组织架构和先进的工具体系及理念 自由与责任并存的企业文化 当前问题: 精选提问 ...

  4. [DFS]排列的生成

    排列的生成 Time Limit:1000MS Memory Limit:65536K Total Submit:150 Accepted:95 Description 输出P(n,m)的排列(n,m ...

  5. 【Java】 5.0 判断与转换

    [概述] 在if/条件语句中,我们已经谈及判断了,这次将详细讲讲一些逻辑判断 基本逻辑 &:且,And,需要二者均为True |:或,Or ,需要二者其一为False即可 ^:异或,XOE,两 ...

  6. php添加excel更新数据表数据

    公司有个需求,是用excel更新数据的,把错误的行列放到一个数组返回出来,正常的数据则插入,且返回数量 1.先需要引入phpspreadsheet,这里使用composer 安装 composer r ...

  7. Leedcode算法专题训练(动态规划)

    递归和动态规划都是将原问题拆成多个子问题然后求解,他们之间最本质的区别是,动态规划保存了子问题的解,避免重复计算. 斐波那契数列 1. 爬楼梯 70. Climbing Stairs (Easy) L ...

  8. IntelliJ IDEA/Android Studio插件开发指南

    前言 目前在为安卓手机QQ做自动化的相关工作,包括UI自动化,逻辑层自动化等.使用到的uiautomator等框架,需要在Android Studio进行编码工作. 其中很多工作如果做到插件化的话,可 ...

  9. linux 更新python3.8

    1 下载源码 地址 选版本下载即可,目前最新为3.8.2版本. 2 解压 tar -zxvf Python-3.8.2.tgz cd Python-3.8.2 3 新建安装目录 安装目录在/usr/l ...

  10. SAMBA 文件共享服务

    samba 通过简单配置就能够实现Linux系统与Windows系统之间的文件共享工作,也可实现Linux与Linux之间的文件共享. 在配置samba前,有个小建议:虚拟机的ip地址最好配置成静态的 ...