问题

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

计算给定二叉树的所有左叶子之和。

3

     / \

   9  20

  /       \

15       7

在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24


Find the sum of all left leaves in a given binary tree.

3

     / \

   9  20

  /       \

15       7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.


示例

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

public class Program {

    public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(5)
}; var res = SumOfLeftLeaves(root);
Console.WriteLine(res); Console.ReadKey();
} public static int SumOfLeftLeaves(TreeNode root) {
var sum = 0;
PreOrder(root, ref sum, false);
return sum;
} public static void PreOrder(TreeNode root, ref int sum, bool left) {
if(root == null) return;
if(left && root.left == null && root.right == null) sum += root.val;
PreOrder(root?.left, ref sum, true);
PreOrder(root?.right, ref sum, false);
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

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

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

5

分析:

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

C#LeetCode刷题之#404-左叶子之和​​​​​​​​​​​​​​(Sum of Left Leaves)的更多相关文章

  1. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  2. [Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  3. [LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)

    题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...

  4. Java实现 LeetCode 404 左叶子之和

    404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...

  5. 【LeetCode】404. 左叶子之和

    404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...

  6. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  7. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  8. #leetcode刷题之路18-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  9. #leetcode刷题之路15-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  10. #leetcode刷题之路1-两数之和

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

随机推荐

  1. Ethical Hacking - GAINING ACCESS(13)

    CLIENT SIDE ATTACKS Backdoor delivery method2 - backdooring exe downloads Backdoor any exe the targe ...

  2. Ethical Hacking - NETWORK PENETRATION TESTING(6)

    Creating a fake access point (honeypot) Fake access points can be handy in many scenarios, one examp ...

  3. 题解 CF 1372 B

    题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...

  4. [spring cloud] -- 服务注册与服务发现篇

    eureka 服务发现客户端 DiscoveryClinet职责(核心) 注册服务无试了到Eureka Server中; 发送新塘更新与Eureka Server的租约: 在服务关闭时从Eureka ...

  5. [spring] -- AOP、IOC、DI篇

    AOP(面向切面编程) 怎么理解这个切面编程的概念及优点? 概念: 横切关注点与业务逻辑相分离,切面能帮助我们模块化横切关注点: 优点: 现在每个关注点都集中于一个地方,而不是分散到多处代码中: 服务 ...

  6. shell 格式化数据,转换为execl

    awk '  BEGIN { OFS="\t"} ;{ $1=$1 ; print $8,$NF} ' >/root/log/aa.xlsx awk '  BEGIN { O ...

  7. python map函数、filter函数、reduce函数

    1.map函数:map(func,可迭代对象): ①func可以是自定义的函数,也可以是功能简单的匿名函数(通过lambda定义) ②处理逻辑:表示将传入的可迭代对象依次循环,将每个元素按照传入的fu ...

  8. python匿名函数和内置函数

    一.匿名函数 匿名函数定义lambda a,b,c:(x,y,z) a.b.c相当于形参,多个形参之间用逗号隔开,多个形参不能用括号括起来 (x.y.z)相当于返回值,多个返回值之间用逗号隔开,多个返 ...

  9. URI(统一资源标识符)

    URI:统一资源标识符 (Uniform Resource Identifier) 统一资源标识符是一个用于标识某一互联网资源名称的字符串. Web上可用的每种资源 -HTML文档.图像.视频片段.程 ...

  10. Letex中表格问题

    最近在学习使用Letex,在学习过程中碰到很多小问题,故记之. 以下是一个参数表的实例(绘成三线表的形式). \begin{table}[hp] %%参数: h:放在此处 t:放在顶端 b:放在底端 ...