LeetCode题目——左叶子之和(sum-of-left-leaves)

计算给定二叉树的所有左叶子之和。

示例:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7
  6. 在这个二叉树中,有两个左叶子,分别是 9 15,所以返回 24

思路

本题的难点在于,首先要知道

  • 什么是叶子节点 ?
  • 如何判断一个节点是不是左叶子节点 ?

第一个问题,

如果一个节点没有左右孩子,那么这个节点就是叶子节点。或者一棵树当中没有子结点(即度为0)的结点称为叶子结点,简称“叶子”。

我们看一下节点的代码TreeNode,如下

  1. public class TreeNode {
  2. public int val;
  3. public TreeNode left;
  4. public TreeNode right;
  5. public TreeNode(int x) {
  6. val = x;
  7. }
  8. }

很明显节点自己就可以判断它的左右孩子是不是空。

第二个问题,

如何判断一个节点是不是左叶子节点,这个只能由它的父亲来判断。

深圳优先搜索

  1. package sum_of_left_leaves;
  2. import common.TreeNode;
  3. //404. 左叶子之和
  4. public class Solution {
  5. public int sumOfLeftLeaves(TreeNode root) {
  6. return root != null ? dfs(root) : 0;
  7. }
  8. //深度优先搜索
  9. public int dfs(TreeNode node) {
  10. int ans = 0;
  11. if (node.left != null) {
  12. ans += isLeafNode(node.left) ? node.left.val : dfs(node.left);
  13. }
  14. //当前节点的右节点不为空,并且不是叶子节点,则继续进行深度优先搜索
  15. if (node.right != null && !isLeafNode(node.right)) {
  16. ans += dfs(node.right);
  17. }
  18. return ans;
  19. }
  20. //判断是否是叶子节点
  21. public boolean isLeafNode(TreeNode node) {
  22. return node.left == null && node.right == null;
  23. }
  24. public static void main(String[] args) {
  25. TreeNode root = new TreeNode(3);
  26. TreeNode root_l = new TreeNode(9);
  27. TreeNode root_r = new TreeNode(20);
  28. TreeNode root_rl = new TreeNode(15);
  29. TreeNode root_rr = new TreeNode(7);
  30. root.left = root_l;
  31. root.right = root_r;
  32. root_r.left = root_rl;
  33. root_r.right = root_rr;
  34. Solution s =new Solution();
  35. int sum = s.sumOfLeftLeaves(root);
  36. System.out.println(sum);
  37. }
  38. }

广度优先搜索

  1. package sum_of_left_leaves;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import common.TreeNode;
  5. class Solution2 {
  6. public int sumOfLeftLeaves(TreeNode root) {
  7. if (root == null) {
  8. return 0;
  9. }
  10. //定义一个队列,把一层的数据添加到队列中。
  11. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  12. queue.offer(root);
  13. int ans = 0;
  14. while (!queue.isEmpty()) {
  15. TreeNode node = queue.poll();
  16. if (node.left != null) {
  17. if (isLeafNode(node.left)) {
  18. ans += node.left.val;
  19. } else {
  20. queue.offer(node.left);
  21. }
  22. }
  23. if (node.right != null) {
  24. if (!isLeafNode(node.right)) {
  25. queue.offer(node.right);
  26. }
  27. }
  28. }
  29. return ans;
  30. }
  31. //判断当前节点是否是叶子节点
  32. public boolean isLeafNode(TreeNode node) {
  33. return node.left == null && node.right == null;
  34. }
  35. }

测试数据

  1. public static void main(String[] args) {
  2. TreeNode root = new TreeNode(3);
  3. TreeNode root_l = new TreeNode(9);
  4. TreeNode root_r = new TreeNode(20);
  5. TreeNode root_rl = new TreeNode(15);
  6. TreeNode root_rr = new TreeNode(7);
  7. root.left = root_l;
  8. root.right = root_r;
  9. root_r.left = root_rl;
  10. root_r.right = root_rr;
  11. Solution s =new Solution();
  12. int sum = s.sumOfLeftLeaves(root);
  13. System.out.println(sum);
  14. }

输出24

一个函数

可以看到,上面的代码都是用了三个函数,那可以用一个函数搞定吗。也是可以的。

  1. class Solution {
  2. public int sumOfLeftLeaves(TreeNode root) {
  3. if (root == null) return 0;
  4. int sum = 0;
  5. if (root.left != null && root.left.left == root.left.right) {
  6. sum = root.left.val;
  7. }
  8. return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
  9. }
  10. }

TreeNode树节点的代码

  1. package common;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. public class TreeNode {
  5. public int val;
  6. public TreeNode left;
  7. public TreeNode right;
  8. public TreeNode(int x) {
  9. val = x;
  10. }
  11. }

左叶子之和(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. WEBAPI 增加身份验证

    1,在Webapi项目下添加如下引用: Microsoft.AspNet.WebApi.Owin Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.S ...

  2. 自编Basic脚本 用BasicIntepreter执行 打印九九乘法表

    源码下载:https://files.cnblogs.com/files/heyang78/BasicInterpreter2-20200601-2.rar 用编程语言打印九九乘法表不难,用自编解释器 ...

  3. 360浏览器最小字号12的坑 -彻底搞清rem

    之前做响应式网站,使用rem作为单位.因为浏览器的默认字号是16px,设置html {font-size: 62.5%; /*10 ÷ 16 × 100% = 62.5%*/},刚好1rem =10p ...

  4. 在 Windows 上安装 Composer

    a.去官网 getcomposer.org 下载安装程序 b.运行安装程序,需要开启三个扩展 openssl.curl.mbstring,没有开启的话 composer 也可以帮助开启:会自动将com ...

  5. 【小程序】---- 封装Echarts公共组件,遍历图表实现多个饼图

    一.问题描述: 在小程序的项目中,封装公共的饼图组件,并在需要的页面引入使用.要求一个页面中有多个饼图,动态渲染不同的数据. 二.效果实现: 1. 查看——小程序使用Echarts的方式 2. 封装饼 ...

  6. C# .Net 委托和事件的区别

    在.net中,事件是一种特殊的委托,那他到底特殊在哪,换句话说,加上event关键字到底有什么用,我理解主要有两方面,下面用实例说明: 一 .事件只能在本类型内部“触发”,委托不管在本类型内部还是外部 ...

  7. jsop之---实现过程

    JSONP(JSONP - JSON with Padding是JSON的一种“使用模式”),利用script标签的src属性(浏览器允许script标签跨域) 跨域访问,非同源访问 <!DOC ...

  8. CTF-Bugku-杂项-21-28

    2020.09.14 下午奥力给 做题 第二十一题 细心的大象 https://ctf.bugku.com/challenges#细心的大象 这一看就是一只有故事的大象,图片详情中有东西,base64 ...

  9. [LeetCode]69. x 的平方根(数学,二分)

    题目 https://leetcode-cn.com/problems/sqrtx 题解 方法一:牛顿迭代法 按点斜式求出直线方程(即过点Xn,f(Xn)),然后求出直线与x轴交点,即为Xn+1: 求 ...

  10. 腾讯云COS对象存储 Web 端直传实践(JAVA实现)

    使用 腾讯云COS对象存储做第三方存储云服务,把一些文件都放在上面,这里主要有三中实现方式:第一种就是在控制台去设置好,直接上传文件.第二种就是走服务端,上传文件,就是说,上传文件是从服务端去上传上去 ...