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:输入: ...
随机推荐
- python利用difflib判断两个字符串的相似度
我们再工作中可能会遇到需要判断两个字符串有多少相似度的情况(比如抓取页面内容存入数据库,如果相似度大于70%则判定为同一片文章,则不录入数据库) 那这个时候,我们应该怎么判断呢? 不要着急,pytho ...
- T3 成绩单 题解
这个题本来不归我讲,但我A完之后觉得太坑了,还是讲一下吧. 首先这个题有个重要的地方:(字典顺序,学号全为小写字母,从小到大排列) 字典序和字典顺序是不一样的!!! 我以为是字典序……,wa了,字典顺 ...
- P1100 高低位切换
这个题很简单 直接用左移位(<<)和右移位(>>)就可以过了 #include<iostream> #include<cstdio> using nam ...
- canvas : 几个入门需要的基本概念
这段时间做项目需要用canvas. 而我在看文档的时候,发现canvas是一个很独立的API:和DOM BOM基本上没什么关系. 在学习canvas的时候需要了解很多概念,否则看某些文档的讲解可能会看 ...
- 设计模式:template method模式
思想:在父类中定义处理流程的框架,在子类中实现具体的处理方法 优点:在父类中定义处理的算法,无需在每个子类中重复编写 继承关系图: 例子: //接口定义 class Parent { public: ...
- Arrays.sort() ----- DualPivotQuicksort
Arrays.sort() ----- DualPivotQuicksort DualPivotQuicksort是Arrays.sort()对基本类型的排序算法,它不止使用了双轴快速排序,还使用了T ...
- Git报错问题集锦
git merge合并时遇上refusing to merge unrelated histories的解决方案 如果git merge合并的时候出现refusing to merge unrelat ...
- python-在python3中使用容联云通讯发送短信验证码
容联云通讯是第三方平台,能够提供短信验证码和语音通信等功能,这里只测试使用短信验证码的功能,因此只需完成注册登录(无需实名认证等)即可使用其短信验证码免费测试服务,不过免费测试服务只能给控制台中指定的 ...
- windy数(数位dp)
https://www.luogu.com.cn/blog/virus2017/shuweidp https://www.luogu.com.cn/problem/P2657 #include < ...
- VS Code小白使用教程
本文来自作者:你不知道的巨蟹 原文链接 https://www.cnblogs.com/tu-0718/p/10935910.html,如有侵权,则可删除. 前言 现在使用Vscode编码的人越来越多 ...