左叶子之和(sum-of-left-leaves)
LeetCode题目——左叶子之和(sum-of-left-leaves)
计算给定二叉树的所有左叶子之和。
示例:
3
/ \
9 20
/ \
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
思路
本题的难点在于,首先要知道
- 什么是叶子节点 ?
- 如何判断一个节点是不是左叶子节点 ?
第一个问题,
如果一个节点没有左右孩子,那么这个节点就是叶子节点。或者一棵树当中没有子结点(即度为0)的结点称为叶子结点,简称“叶子”。
我们看一下节点的代码TreeNode
,如下
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
}
}
很明显节点自己就可以判断它的左右孩子是不是空。
第二个问题,
如何判断一个节点是不是左叶子节点,这个只能由它的父亲来判断。
深圳优先搜索
package sum_of_left_leaves;
import common.TreeNode;
//404. 左叶子之和
public class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return root != null ? dfs(root) : 0;
}
//深度优先搜索
public int dfs(TreeNode node) {
int ans = 0;
if (node.left != null) {
ans += isLeafNode(node.left) ? node.left.val : dfs(node.left);
}
//当前节点的右节点不为空,并且不是叶子节点,则继续进行深度优先搜索
if (node.right != null && !isLeafNode(node.right)) {
ans += dfs(node.right);
}
return ans;
}
//判断是否是叶子节点
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
TreeNode root_l = new TreeNode(9);
TreeNode root_r = new TreeNode(20);
TreeNode root_rl = new TreeNode(15);
TreeNode root_rr = new TreeNode(7);
root.left = root_l;
root.right = root_r;
root_r.left = root_rl;
root_r.right = root_rr;
Solution s =new Solution();
int sum = s.sumOfLeftLeaves(root);
System.out.println(sum);
}
}
广度优先搜索
package sum_of_left_leaves;
import java.util.LinkedList;
import java.util.Queue;
import common.TreeNode;
class Solution2 {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
//定义一个队列,把一层的数据添加到队列中。
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int ans = 0;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.left != null) {
if (isLeafNode(node.left)) {
ans += node.left.val;
} else {
queue.offer(node.left);
}
}
if (node.right != null) {
if (!isLeafNode(node.right)) {
queue.offer(node.right);
}
}
}
return ans;
}
//判断当前节点是否是叶子节点
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null;
}
}
测试数据
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
TreeNode root_l = new TreeNode(9);
TreeNode root_r = new TreeNode(20);
TreeNode root_rl = new TreeNode(15);
TreeNode root_rr = new TreeNode(7);
root.left = root_l;
root.right = root_r;
root_r.left = root_rl;
root_r.right = root_rr;
Solution s =new Solution();
int sum = s.sumOfLeftLeaves(root);
System.out.println(sum);
}
输出24
一个函数
可以看到,上面的代码都是用了三个函数,那可以用一个函数搞定吗。也是可以的。
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int sum = 0;
if (root.left != null && root.left.left == root.left.right) {
sum = root.left.val;
}
return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
}
TreeNode树节点的代码
package common;
import java.util.LinkedList;
import java.util.Queue;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
}
}
左叶子之和(sum-of-left-leaves)的更多相关文章
- [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. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- LeetCode404Sum of Left Leaves左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...
- [LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)
题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...
- 【LeetCode】404. 左叶子之和
404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...
- 【leetcode 简单】 第九十四题 左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 # Definition for a binary ...
- Java实现 LeetCode 404 左叶子之和
404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...
- 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 ...
- [leetcode]404. Sum of Left Leaves左叶子之和
弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...
随机推荐
- WEBAPI 增加身份验证
1,在Webapi项目下添加如下引用: Microsoft.AspNet.WebApi.Owin Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.S ...
- 自编Basic脚本 用BasicIntepreter执行 打印九九乘法表
源码下载:https://files.cnblogs.com/files/heyang78/BasicInterpreter2-20200601-2.rar 用编程语言打印九九乘法表不难,用自编解释器 ...
- 360浏览器最小字号12的坑 -彻底搞清rem
之前做响应式网站,使用rem作为单位.因为浏览器的默认字号是16px,设置html {font-size: 62.5%; /*10 ÷ 16 × 100% = 62.5%*/},刚好1rem =10p ...
- 在 Windows 上安装 Composer
a.去官网 getcomposer.org 下载安装程序 b.运行安装程序,需要开启三个扩展 openssl.curl.mbstring,没有开启的话 composer 也可以帮助开启:会自动将com ...
- 【小程序】---- 封装Echarts公共组件,遍历图表实现多个饼图
一.问题描述: 在小程序的项目中,封装公共的饼图组件,并在需要的页面引入使用.要求一个页面中有多个饼图,动态渲染不同的数据. 二.效果实现: 1. 查看——小程序使用Echarts的方式 2. 封装饼 ...
- C# .Net 委托和事件的区别
在.net中,事件是一种特殊的委托,那他到底特殊在哪,换句话说,加上event关键字到底有什么用,我理解主要有两方面,下面用实例说明: 一 .事件只能在本类型内部“触发”,委托不管在本类型内部还是外部 ...
- jsop之---实现过程
JSONP(JSONP - JSON with Padding是JSON的一种“使用模式”),利用script标签的src属性(浏览器允许script标签跨域) 跨域访问,非同源访问 <!DOC ...
- CTF-Bugku-杂项-21-28
2020.09.14 下午奥力给 做题 第二十一题 细心的大象 https://ctf.bugku.com/challenges#细心的大象 这一看就是一只有故事的大象,图片详情中有东西,base64 ...
- [LeetCode]69. x 的平方根(数学,二分)
题目 https://leetcode-cn.com/problems/sqrtx 题解 方法一:牛顿迭代法 按点斜式求出直线方程(即过点Xn,f(Xn)),然后求出直线与x轴交点,即为Xn+1: 求 ...
- 腾讯云COS对象存储 Web 端直传实践(JAVA实现)
使用 腾讯云COS对象存储做第三方存储云服务,把一些文件都放在上面,这里主要有三中实现方式:第一种就是在控制台去设置好,直接上传文件.第二种就是走服务端,上传文件,就是说,上传文件是从服务端去上传上去 ...