问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4078 访问。

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

说明: 叶子节点是指没有子节点的节点。

给定如下二叉树,以及目标和 sum = 22,

5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。


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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4078 访问。

public class Program {public static void Main(string[] args) {var root = new TreeNode(1) {left = new TreeNode(2) {right = new TreeNode(5)},right = new TreeNode(3)};var res = HasPathSum(root, 4);Console.WriteLine(res);res = HasPathSum2(root, 5);Console.WriteLine(res);Console.ReadKey();}public static bool HasPathSum(TreeNode root, int sum) {if(root == null) return false;var res = false;PathSum(root, root.val, sum, ref res);return res;}public static void PathSum(TreeNode root, int cursum, int sum, ref bool has) {if(root.left == null && root.right == null && cursum == sum) {//若左右子节点都为空,本次路径结束has = true;return;}if(root.left != null)//若左子节点不为空,则将左子节点和当前和累加之后参与下次递归PathSum(root.left, cursum + root.left.val, sum, ref has);if(root.right != null)//若右子节点不为空,则将右子节点和当前和累加之后参与下次递归PathSum(root.right, cursum + root.right.val, sum, ref has);}public static bool HasPathSum2(TreeNode root, int sum) {//边界判定if(root == null) return false;//如果 sum 等于当前节点的值,并且为叶子节点时,返回 trueif(root.val == sum && root.left == null && root.right == null) return true;//每次传递时用 sum - 当前值return HasPathSum2(root.left, sum - root.val) ||HasPathSum2(root.right, sum - root.val);}public class TreeNode {public int val;public TreeNode left;public TreeNode right;public TreeNode(int x) { val = x; }}}

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4078 访问。

True
False

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#112-路径总和​​​​​​​(Path Sum)相关推荐

  1. leetcode题库--112路径总和

    路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 思路: 思路比较简单,就是递归将前面的和传 ...

  2. leetcode刷题:不同路径II

    分析: 我们用 f(i, j)f(i,j) 来表示从坐标 (0, 0)(0,0) 到坐标 (i, j)(i,j) 的路径总数,u(i, j)u(i,j) 表示坐标 (i, j)(i,j) 是否可行,如 ...

  3. leetcode刷题:不同路径

    题目: 分析:因为题目要求只能向右和向下走动一格. 经过分析可得如下代码 从左上角到右下角的过程中,我们需要移动 m+n-2m+n−2 次,其中有 m-1m−1 次向下移动,n-1n−1 次向右移动. ...

  4. C#LeetCode刷题之#40-组合总和 II(Combination Sum II)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3666 访问. 给定一个数组 candidates ...

  5. C#LeetCode刷题之#39-组合总和(Combination Sum)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3663 访问. 给定一个无重复元素的数组 candi ...

  6. C#LeetCode刷题之#404-左叶子之和​​​​​​​​​​​​​​(Sum of Left Leaves)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3      / ...

  7. C#LeetCode刷题之#371-两整数之和(Sum of Two Integers)

    问题 不使用运算符 + 和 - ,计算两整数 a .b 之和. 输入: a = 1, b = 2 输出: 3 输入: a = -2, b = 3 输出: 1 Calculate the sum of ...

  8. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 22.2% ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

最新文章

  1. Magicodes.WeiChat——自定义knockoutjs template、component实现微信自定义菜单
  2. CentOS 6.5 安装与配置LAMP
  3. KUR-Couriers
  4. 正则表达式的固化分组
  5. mfc中怎么集成文件_怎么把几个pdf合并并打印在一份文件中?
  6. 中国剧本推理市场洞察2021
  7. 7-2 最大流 加强版 (20 分)
  8. 利用flashback query解决误删除表数据
  9. 使用元组交换两个数据
  10. 机器人正运动学(7)—— 连杆坐标系与DH参数
  11. 确定有限状态自动机(deterministic finite automaton --> DFA)
  12. 20行Python代码爬取LOL全英雄皮肤图片
  13. 计算机图形学课程设计内容及要求
  14. 2022-06-26 笔记本新机重装系统
  15. 邮件传输的过程都看不懂。那我走(狗头)
  16. 利用Python将视频文件转成语音文件
  17. 破晓博客-自定义标签的开发
  18. Matlab将图像序列合成为视频
  19. 从初试北京211到走上调剂的坎坷考研路
  20. u-boot-1.3.4 移植到S3C2440 (带有某些解析)

热门文章

  1. LeetCode 581. Shortest Unsorted Continuous Subarray
  2. AttributeError : module ‘enum‘ has no attribute ‘IntFlag‘
  3. 【今日CV 计算机视觉论文速览 第127期】Fri, 7 Jun 2019
  4. 还发愁项目经验吗?基于Netty实现分布式RPC框架[附完整代码]
  5. 【Linux学习】GDB调试器基本命令必知必会(一)
  6. 【Linux入门学习之】ubuntu10.04 ruijie配置上网(用mentohust)
  7. 输出一行星花 1110 java
  8. 安装PostgreSQL单机版
  9. 阿里云如何二次驱动云计算
  10. 基于 Raphael 的 Web UI 设计 - 初稿