C#LeetCode刷题之#404-左叶子之和(Sum of Left Leaves)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 7There 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)的更多相关文章
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- [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 ...
- [LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)
题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...
- Java实现 LeetCode 404 左叶子之和
404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...
- 【LeetCode】404. 左叶子之和
404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- #leetcode刷题之路18-四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- #leetcode刷题之路1-两数之和
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
随机推荐
- Go Pentester - HTTP Servers(3)
Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...
- patelinux 安装
参考文档:https://china.xilinx.com/support/documentation/sw_manuals/xilinx2017_2/ug1144-petalinux-tools-r ...
- paypal支付平台如何使用二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 paypal支付平台如何使用二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的优势 ...
- git pull & git fetch
Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin mastergit log ...
- 自动化不知如何参数化(二)?xlrd来帮你解决
在昨天的博文中介绍了普通单元格数据的获取,以及单元格数据类型的转换,详细见博文:自动化不知如何参数化(一)?xlrd来帮你解决. 昨天的那篇博文中,还有个获取合并单元格数据的问题没解决,今天就专门来讲 ...
- nrm安装使用(mac)
在开发工作中时常有需要切换npm源的需求以及更换node版本的情况,这两种情况都有对应的管理器来使用 一.nrm nrm是一个npm源管理工具,使用它可以快速切换npm源. 1.nrm安装(全局安装) ...
- jmeter之断言复制过来的内容也会失败
今天遇到个很纳闷的事儿,就决定记下来,在做jmeter断言的时候,明明我是从相应文本中拷贝出来的内容,但是依旧会断言失败,差了很多资料无果,最终请教了大佬才发现是特殊字符的问题 jmeter断言中不会 ...
- LQB2018A09倍数问题
这个题,第一反应一定是三个for嵌套加一个max比较. 超级无敌大暴搜 #include <iostream> #include <string> #include <s ...
- Mac Sourcetree克隆项目提示无效的url
之前用SoucreTree拉去过另一个账号的git项目,今天创建了一个新的码云账号,克隆里面的项目是一直报错误 > 错误如下: > 原因以及解决方案:
- 线程_gevent自动切换CPU协程
import gevent def f(n): for i in range(n): print (gevent.getcurrent(), i) # gevent.getcurrent() 获取当前 ...