问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 等于当前节点的值,并且为叶子节点时,返回 true
if(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. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  2. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

  3. LeetCode刷题笔记-递归-路径总和

    题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...

  4. leetcode刷题-62不同路径2

    题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在 ...

  5. leetcode刷题-64最小路径和

    题目 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入:[  [1,3,1],  [1,5, ...

  6. leetcode刷题-71简化路径

    题目 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (..) 表示将目录切换到 ...

  7. leetcode刷题-62不同路径

    题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问总 ...

  8. Leetcode刷题6—不同路径

    一.要求 二.知识点 这道题属于动态规划,主要思路就是将大问题不断分解成小问题进行求解 三.解题思路 1.利用数学思路找规律 将m和n组合成一个m*n的矩阵,而且是一个对称阵 [[0],[1],[1] ...

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

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

随机推荐

  1. 什么是A站、B站、C站、D站、E站、F站、G站、HIJKLM站N站?

    A站AcFun弹幕视频网,简称“A站”,成立于2007年6月,取意于Anime Comic Fun,是中国大陆第一家弹幕视频网站.A站以视频为载体,逐步发展出基于原生内容二次创作的完整生态,拥有高质量 ...

  2. Ethical Hacking - Web Penetration Testing(10)

    SQL INJECTION SQLMAP Tool designed to exploit SQL injections. Works with many DB types, MySQL, MSSQL ...

  3. Ethical Hacking - Web Penetration Testing(9)

    SQL INJECTION Discovering SQLi in GET Inject by browser URL. Selecting Data From Database Change the ...

  4. OSCP Learning Notes - Capstone(3)

    DroopyCTF Walkthrough Preparation: Download the DroopyCTF virtual machine from the following website ...

  5. Python Ethical Hacking - Malware Packaging(3)

    Convert Python Programs to OS X Executables https://files.pythonhosted.org/packages/4a/08/6ca123073a ...

  6. lua的table表去重

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321  我的个人博客 方法一 用过lua的人都知道,lua的table中不允许存在相同的key,利用这个思想,我们可以将原始table ...

  7. 题解 CF296B 【Yaroslav and Two Strings】

    题目 传送门 题目大意 如果两个只包含数字且长度为 \(n\) 的字符串 \(s\) 和 \(w\) 存在两个数字 \(1≤i,j≤n\),使得 \(s_i<w_i,s_j>w_j\) , ...

  8. 微信小程序实战:app主页面保存page页面实例

    先上代码. app.js //app.js App({ onLaunch: function () { // 登录 wx.login({ success: res => { if (this.g ...

  9. vue 应用 :关于 ElementUI 的 message 组件

    我们知道,这个东西的基本用法是这样的: this.$message({ message: '恭喜你,这是一条成功消息', type: 'success' }); 但是我觉得这样还是有点麻烦,所以我决定 ...

  10. 设计模式:singleton模式

    目的:限制类的实例个数只能是一个 例子: #define AGT_DECLARE_SINGLETON(ClassName) \ public: \ static ClassName *Instance ...