LeetCode_404. Sum of Left Leaves
404. 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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
package leetcode.easy; /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class SumOfLeftLeaves {
public int sumOfLeftLeaves(TreeNode root) {
if (null == root) {
return 0;
}
if (root.left != null && root.left.left == null && root.left.right == null) {
return root.left.val + sumOfLeftLeaves(root.right);
} else {
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.print(sumOfLeftLeaves(tn11));
}
}
LeetCode_404. Sum of Left Leaves的更多相关文章
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- [LeetCode] 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. 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 Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
- 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 ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...
- LeetCode 404. 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 ...
随机推荐
- 如何识别和解决SQL Server中的热闩锁(PAGELATCH_EX)
描述 在SQL Server中,内部闩锁体系结构可在SQL操作期间保护内存.通过页面上的读写操作,可以确保内存结构的一致性.从根本上讲,它具有两个类:缓冲区锁存器和非缓冲区锁存器,它们在SQL Eng ...
- learning java AWT 画图
import javax.swing.*; import java.awt.*; import java.util.Random; public class SimpleDraw { private ...
- Problem 5 素数筛法+并查集
$des$ 给定一个长度为 $n$ 的正整数序列 ${a_i }$.将 ${1,2,...,n}$ 划分成两个非空集合 $S.T$,使得 $gcd(\prod_{i \in S} a_i, \prod ...
- Jmeter 5.1实现图片上传接口测试
背景: 项目过程中需要抓取接口进行图片上传的接口测试,所有上传功能大同小异,无非就是参数内容不同,此处记录一下,为其他上传做一些参考 1.通过fiddler抓取到的参数如下: Content-Disp ...
- SQL数据清洗
大家好,我是jacky,很高兴继续跟大家分享<MySQL数据分析实战>,从本节课程开始,我们的课程就会变得越来越实战,也会越来越有意思了: 我们课程的主体叫MySQL数据分析实战,那我们用 ...
- 理解 ES6 语法中 yield* 关键字的作用
在 ES6 中新增了生成器函数的语法,本文解释了与生成器函数有关的 yield* 关键字,及其使用场景. 描述 根据语法规范,yield* 的作用是代理 yield 表达式,将需要函数本身产生(yie ...
- docker安装postgresql
1.在linux执行以下代码: docker run -p : -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES ...
- Tkinter 之磁盘搜索工具实战
一.效果图 二.代码 miniSearch.py from tkinter import * from tkinter import ttk, messagebox, filedialog from ...
- Python操作MySQL数据库(步骤教程)
我们经常需要将大量数据保存起来以备后续使用,数据库是一个很好的解决方案.在众多数据库中,MySQL数据库算是入门比较简单.语法比较简单,同时也比较实用的一个.在这篇博客中,将以MySQL数据库为例,介 ...
- windows 共享文件夹,和共享打印机
达成的情形,目标主机上登陆用户设置密码,其他pc上需要目标主机的用户和密码才能访问其共享文件夹 步骤:1.目标主机,设置文件夹共享 在文件夹上右键-属性,点击共享选项卡,然后点击共享按钮,继续点 ...