左叶子之和(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 ...
随机推荐
- 实操ES6之Promise
箭头函数和this 写Promise的时候,自然而然会使用箭头函数的编写方式.箭头函数就是.Neter们熟知的lambda函数,已经被大部分主流语言支持,也受到了广大码农的交口称赞,但是Jser们却会 ...
- 小程序开发-基础组件icon/text/progress入门
小程序的基础组件--基础内容 基础内容分为三大组件: 1. icon--图标 index.wxml <view class="group"> <block wx: ...
- [Python]打印指定目录下所有子目录
import os for root,dirs,files in os.walk(r"/home/os-hy01"): for dir in dirs: print(dir) -- ...
- 教会舍友玩 Git (再也不用担心他的学习)
舍友长大想当程序员,我和他爷爷奶奶都可高兴了,写他最喜欢的喜之郎牌Git文章,学完以后,再也不用担心舍友的学习了(狗头)哪里不会写哪里 ~~~ 一 先来聊一聊 太多东西属于,总在用,但是一直都没整理的 ...
- jmeter服务器监控磁盘IO、网络-PerfMon Metrics Collector
1.jmeetr客户端安装jp@gc - PerfMon Metrics Collector 先安装jmeter-plugins 启动jmeter ——> 客户端选项 ——> jmeter ...
- Oracle数据库之体系结构
Oracle数据库管理系统中的3个重要的概念:实例(Instance).数据库(Database)和数据库服务器(Database Server). 实例:是后台进程和内存结构的集合,是Oracle数 ...
- Java数据类型之Cache模式
1.关于Java数据类型 基本数据类型 基本数据类型有8种,每种基本数据类型都有对应的引用类型. 类型 描述 长度 可表示数据 包装类型 boolean 布尔型 1 true.false Boolea ...
- Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage
一,引言 上一篇文章我们在.NET 项目中添加了 “WindowsAzure.Storage” 的 NuGet 包进行操作Table 数据,但是使用的 “WindowsAzure.Storage” ...
- python调用接口——requests模块
前提:安装pip install requests 导入import requests 1.get请求 result=requests.get(url,d).json() 或 .text 2. ...
- 论文:Show and Tell: A Neural Image Caption Generator-阅读总结
Show and Tell: A Neural Image Caption Generator-阅读总结 笔记不能简单的抄写文中的内容,得有自己的思考和理解. 一.基本信息 标题 作者 作者单位 发表 ...