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)的更多相关文章

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

  2. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  3. LeetCode404Sum of Left Leaves左叶子之和

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

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

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

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

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

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

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

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

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

  8. 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 ...

  9. [leetcode]404. Sum of Left Leaves左叶子之和

    弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...

随机推荐

  1. 跟着兄弟连系统学习Linux-【day01】

    day01-20200527 p1.unix发展历史         (1960,有一个实验室,三个团队组成,开发了Unix雏形,但是因为没有办法发版,所以就荒废了.这个小组里面有一个人,打游戏的时候 ...

  2. java 检查进程是否存在

    以nginx进程为例子 private final static String NAME_STRING = "nginx.exe"; //传入进程名称processName pub ...

  3. Java枚举解读

    Java枚举 枚举类概念的理解与定义 一个类的对象是有限个,确定的,我们称此为枚举类. 当需要定义和维护一组常量时,强烈建议使用枚举类. 如果一个枚举类中只有一个对象,则可以作为单例模式的实现方式. ...

  4. CTF-BugKu-杂项-29-33

    2020.09.15 今天换个新壁纸,换个新背景音乐,燃起来 做题 第二十九题 论剑 https://ctf.bugku.com/challenges#论剑 图片详情没啥信息,不是正方形,考虑改成正方 ...

  5. JavaScript 伪Ajax请求

    伪Ajax 通过iframe以及form表单,可以实现伪Ajax的方式. 并且它的兼容性是最好的. iframe iframe标签能够获取一个其他页面的文档内容,这说明它内部肯定是发送了一个请求,并且 ...

  6. SpringCloud实战 | 第三篇:SpringCloud整合Nacos实现配置中心

    前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...

  7. apache OS 10013 问题

    问题描述 (OS 10013)通常每个套接字地址 (协议/网络地址/端口) 只允许使用一次: make_sock: could not bind to address 0.0.0.0:80 解决方法 ...

  8. Node.js 从零开发 web server博客项目[登录]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  9. SM4密码算法matlab实现

    %function C=SM4(X,K,M)%M为1时进行加密,M为0时进行解密操作,X为明文/密文输入,K为密钥输入X='0123456789abcdeffedcba9876543210';%X=' ...

  10. 容器云平台No.7~kubernetes监控系统prometheus-operator

    简介 prometheus-operator Prometheus:一个非常优秀的监控工具或者说是监控方案.它提供了数据搜集.存储.处理.可视化和告警一套完整的解决方案.作为kubernetes官方推 ...