LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和
404. Sum of Left Leaves
LeetCode404. Sum of Left Leaves
题目描述
计算给定二叉树的所有左叶子之和。
示例:
3
/ \
9 20
/ \
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24。
Java 实现
TreeNode 结构
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Recursive
class Solution {
private int sum = 0;
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left != null && root.left.left == null && root.left.right == null) {
sum += root.left.val;
}
sumOfLeftLeaves(root.left);
sumOfLeftLeaves(root.right);
return sum;
}
}
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int count = 0;
if (root == null) {
return 0;
}
if (root.left != null) {
if (root.left.left == null && root.left.right == null) {
count += root.left.val;
} else {
count += sumOfLeftLeaves(root.left);
}
}
count += sumOfLeftLeaves(root.right);
return count;
}
}
Iterative
import java.util.Stack;
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int count = 0;
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return 0;
}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null) {
if (node.left.left == null && node.left.right == null) {
count += node.left.val;
} else {
stack.push(node.left);
}
}
if (node.right != null) {
if (node.right.left != null || node.right.right != null) {
stack.push(node.right);
}
}
}
return count;
}
}
主测试类
public class Test {
public static void main(String[] args) {
Solution tree = new Solution();
/* create a tree */
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
System.out.println(tree.sumOfLeftLeaves(root));
}
}
运行结果
相似题目
参考资料
- https://leetcode.com/problems/sum-of-left-leaves/
- https://leetcode-cn.com/problems/sum-of-left-leaves/
LeetCode 404. 左叶子之和(Sum of Left Leaves)的更多相关文章
- [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 ...
- [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. 左叶子之和
404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...
- LeetCode: 404.左叶子节点
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父 ...
- 左叶子之和(sum-of-left-leaves)
LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...
- 【leetcode 简单】 第九十四题 左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 # Definition for a binary ...
- LeetCode404Sum of Left Leaves左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...
- LC: 404.左叶子节点
计算给定二叉树的所有左叶子之和. 示例: / \ 9 20 / \ 15 7 ,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父节点的左子节点 方法一:用栈,dfs遍历,用全局变量r ...
随机推荐
- 《挑战30天C++入门极限》C++的iostream标准库介绍(1)
C++的iostream标准库介绍(1) 我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的stdio库不同 ...
- Spark(二)—— 标签计算、用户画像应用
一.标签计算 数据 86913510 {"reviewPics":[],"extInfoList":null,"expenseList":n ...
- DB2通过某列分组来去重
DB2通过某列分组来去重,可防止distinct对大字段的去重报错. row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分 ...
- (二)SQL学习之数据定义类SQL
以mysql为例 对数据库的常用操作 创建数据库:CREATE DATABASE mydb; 删除数据库:DROP DATABASE mydb; 切换数据库:USE mydb; 查询数据库:SHOW ...
- linux下的/dev/shm/及对Oracle 的影响
一./dev/shm/介绍: /dev/shm/是linux下一个非常有用的目录,因为这个目录不在硬盘上,而是在内存里.因此在linux下,就不需要大费周折去建ramdisk,直接使用/dev/shm ...
- 微信小程序网络通信(一)
本文链接:https://blog.csdn.net/melovemingming/article/details/82831749微信小程序网络服务器网络配置支持request 普通网络请求.支持套 ...
- Shell脚本自动重启Java服务
话不多说直接上代码: cd /home/javaProduct/if [ -d '/home/javaProduct/lib_new/' ]; thenecho 'Has New Lib!'echo ...
- nodejs设置淘宝镜像
nodeJS的资源仓库在国内使用过程中,偶尔会遇到各种资源问题,通常设置为淘宝的镜像,网上很多说法是安装淘宝镜像,即$ npm install -g cnpm --registry=https://r ...
- win10下caffe+anaconda+python+Jupyter Notebooks安装流程
python3.5(推荐)或者python2.7 CUDA 8+ cuDNN5.1 python环境不能单独配置,必须先编译caffe,才能编译python环境. 下载caffe prebuild版本 ...
- Kotlin从零到精通Android开发
作者 博客地址 https://blog.csdn.net/aqi00 最新源代码 https://github.com/aqi00/kotlin 资源下载和内容勘误 https://blog.cs ...