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));
}
}

运行结果

24

相似题目

参考资料

LeetCode 404. 左叶子之和(Sum of Left Leaves)的更多相关文章

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

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

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

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

  3. [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 ...

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

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

  5. LeetCode: 404.左叶子节点

    计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父 ...

  6. 左叶子之和(sum-of-left-leaves)

    LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...

  7. 【leetcode 简单】 第九十四题 左叶子之和

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

  8. LeetCode404Sum of Left Leaves左叶子之和

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

  9. LC: 404.左叶子节点

    计算给定二叉树的所有左叶子之和. 示例: / \ 9 20 / \ 15 7 ,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父节点的左子节点 方法一:用栈,dfs遍历,用全局变量r ...

随机推荐

  1. SpringMVC处理请求释放静态资源的三种方式

    方式一 在SpringMVC的配置文件中添加如下语句 <mvc:default-servlet-handler/> 说明:当SpringMVC前端控制器对静态资源进行拦截后,在通过处理器映 ...

  2. 小程序中嵌套的h5页面设置分享转发

    场景描述:当在小程序中打开h5页面时,希望小程序的转发出去的标题,图片,跳转link可以通过h5通信实现自定义. 实现方式:通过h5给小程序通信,发送标题,图片,跳转link等信息,让小程序设置分享. ...

  3. python 二维码

    pip3 install Pillow pip3 install qrcode import qrcode text ="gisoracle我爱你呀" #input("输 ...

  4. At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger fo

    一.文章前言 本文是亲测有效解决At least one JAR was scanned for TLDs yet contained no TLDs问题,绝对不是为了积分随便粘贴复制然后压根都没有用 ...

  5. Spring 源码学习之环境搭建

    一.下载Spring 源码 进入 https://github.com/spring-projects/spring-framework/tags 选择下载spring freamework的版本 h ...

  6. Mosquitto配置----日志设置

    https://blog.csdn.net/u012377333/article/details/71101725 # ======================================== ...

  7. mongodb的开机自启动

    一.背景 Linux轻松的在rc.local中写上启动脚本,reboot~发现没有启动成功.这不科学啊,查看日志发现“permission denied” 二.解决 Linux系统下,使用自定配置文件 ...

  8. java泛型--问号?和T或E或K或V的区别

    所谓泛型,就是在定义类.接口.方法.参数或成员变量的时候,指定它们操作对象的类型为通用类型. 使用 尖括号 <> 操作符 (The diamond operator )表示泛型, 尖括号内 ...

  9. 【转载】 clusterdata-2011-2 谷歌集群数据分析(二)--task_usage

    原文地址: https://blog.csdn.net/yangss123/article/details/78298749 由于原文声明其原创文章不得允许不可转载,故这里没有转载其正文内容. --- ...

  10. .frm文件怎么导入到数据库

    如题想搞个私服游戏,但是数据库文件按文档的操作方法行不通.只能自行导入. 其实.frm文件就是mysql表结构文件,你拷贝data那一块的文件到你电脑安装的mysql的data文件下就行了. 一.首先 ...