要求

  • 给出一个二叉树及数字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. 提高Python的性能

    01 使用哈希表的数据结构   如果在程序中遇到大量搜索操作时,并且数据中没有重复项,则可以使用查找而不是循环.举例如下: items = ['a', 'b',..,'100m'] #1000s of ...

  2. C. 【例题3】畜栏预定

    C . [ 例 题 3 ] 畜 栏 预 定 C. [例题3]畜栏预定 C.[例题3]畜栏预定 题解 考虑贪心 Code #include <bits/stdc++.h> using nam ...

  3. OO UNIT 1 个人单元总结

    面向对象课程--第一单元个人总结 作业分析 第一次作业 概要 本次作业主要对简单幂函数的多项式进行求导计算,要点在于对输入字符串的处理,利用正则表达式匹配即可,并且需要对输出表达式的长度进行优化. 度 ...

  4. Java代码度量分析工具:DesigniteJava简介

    前言 在Java面向对象课程的学习过程中,我们需要使用度量工具来分析自己程序的代码结构.受OO课程组以及前辈们博客提醒,笔者找到了DesigniteJava这款软件,现对此软件进行简单的说明. 一.D ...

  5. JMeter5.4.1源码IDEA构建&二次开发(实战)

    JMeter5.4.1源码IDEA构建&二次开发(实战) 目录 JMeter5.4.1源码IDEA构建&二次开发(实战) 1.下载源码 2.导入IDEA 2.1 先设置Gradle目录 ...

  6. Recoil Input 光标位置被重置到末尾的问题

    考察如下代码,页面中有个输入框,通过 Recoil Atom 来存储输入的值. App.tsx function NameInput() { const [name, setName] = useRe ...

  7. Linux 用户和用户组管理(useradd userdel groupadd groupdel)

    Linux 用户和用户组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. Linux系统用户账户的 ...

  8. lvs 负载均衡 _DR模式 _Python脚本

    import paramiko vip='192.168.83.6' # 虚拟IP # direct_server_information ds_info={ 'ip':'192.168.83.5', ...

  9. 请求转发(forward)和请求包含(include)的区别?

    请求包含的例子 第一个Servlet (DispatcherServlet) @Override protected void doGet(HttpServletRequest req, HttpSe ...

  10. 面试系列<5>——面向对象

    面试系列--面向对象思想 一.三大特性 封装 利用抽象数据类型将数据和基于数据的操作封装在一起,使其成为一个不可分割的独立实体.数据被保护在抽象数据类型内部,尽可能地隐藏内部细节,只保留一些对外的接口 ...