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. 《挑战30天C++入门极限》C++中类的多态与虚函数的使用

        C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...

  2. Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist

    目录 Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist 解决方 ...

  3. (持续更新)vs2012,2013,2015,2017,2019 常用的插件 与 开发中常用的工具

    这篇博客 持续更新. 小伙伴们可以复制名称,在vs的扩展和更新中去搜索下载 .其他的工具在官网下载

  4. Java 多线程之生产者消费者(多个生成者多个消费者)synchronized 和lock多线程通讯和同步实现

    public class ProducterConsumerSample { public static void main(String[] args) { Resourse res = new R ...

  5. boosting与随机森林

      本文原创,转载请注明出处 http://www.cnblogs.com/gufeiyang 本文主要分两部分,boosting 与 随机森林. “三个臭皮匠顶一个诸葛亮”是说三个不聪明的人集合在一 ...

  6. Android根据内网外网连接情况配置服务器访问IP

    新项目的app,可通过内网和外网的服务器ip进行请求访问,但是客户提供了专业终端,终端在wifi情况下走外网内网都可以,但关闭wifi则只能走4G专网,也就是只能走内网. 可前往我的小站查看:Andr ...

  7. Oracle 11g 数据库 expdp/impdp 全量导入导出

    从一个用户导出导入到另一个用户 问题 环境:oracle 11g; redhat 6 usera是具有DBA权限,密码为usera 全量导出usera用户下的所有内容,并导入到新建的userb用户 解 ...

  8. deepin常用软件列表

    deepin常用软件列表 软件列表 Safe Eyes 视力保护程序 网址

  9. Flutter -------- BottomNavigationBar 界面切换

    Android 中有BottomNavigationBar+Fragment切换 而在Flutter也有的BottomNavigationBar 效果图 底部有两种情况 底部导航栏的类型更改其项目的显 ...

  10. 完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题

    ①:例如很多人第一步就会这样做: 出现:Cannot download "https://github.com/sass/node-sass/releases/download/版本号/XX ...