• 【LeetCode每日一题】——404.左叶子之和


    一【题目类别】

    • 二叉树

    二【题目难度】

    • 简单

    三【题目编号】

    • 404.左叶子之和

    四【题目描述】

    • 给定二叉树的根节点 root ,返回所有左叶子之和。

    五【题目示例】

    • 示例 1:
      在这里插入图片描述
      输入: root = [3,9,20,null,null,15,7]
      输出: 24
      解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

    • 示例 2:
      输入: root = [1]
      输出: 0

    六【题目提示】

    • 节点数在 [1, 1000] 范围内
    • -1000 <= Node.val <= 1000

    七【解题思路】

    • 这个题思路比较简单,首先想到递归,但是应该怎么进行递归呢?
    • 首先我们要求的节点应该是左子树节点
    • 其次这个左子树节点还应该是叶子节点
    • 基于以上两点进行判断,将其求和返回即可

    八【时间频度】

    • 时间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为树的结点个数
    • 空间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为树的结点个数

    九【代码实现】

    1. Java语言版
    package Tree;
    
    public class p404_SumOfLeftLeaves {
    
        int val;
        p404_SumOfLeftLeaves left;
        p404_SumOfLeftLeaves right;
    
        public p404_SumOfLeftLeaves() {
        }
    
        public p404_SumOfLeftLeaves(int val) {
            this.val = val;
        }
    
        public p404_SumOfLeftLeaves(int val, p404_SumOfLeftLeaves left, p404_SumOfLeftLeaves right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    
        public static void main(String[] args) {
            p404_SumOfLeftLeaves root = new p404_SumOfLeftLeaves(3);
            p404_SumOfLeftLeaves left = new p404_SumOfLeftLeaves(9);
            p404_SumOfLeftLeaves right = new p404_SumOfLeftLeaves(20);
            p404_SumOfLeftLeaves right1 = new p404_SumOfLeftLeaves(15);
            p404_SumOfLeftLeaves right2 = new p404_SumOfLeftLeaves(7);
            root.left = left;
            root.right = right;
            right.left = right1;
            right.right = right2;
            int res = sumOfLeftLeaves(root);
            System.out.println("res = " + res);
        }
    
        public static int sumOfLeftLeaves(p404_SumOfLeftLeaves root) {
            if (root == null) {
                return 0;
            }
            int res = 0;
            if (root.left != null && root.left.left == null && root.left.right == null) {
                res += root.left.val;
            }
            return res + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    1. C语言版
    #include
    
    struct TreeNode
    {
    	int val;
    	struct TreeNode *left;
    	struct TreeNode *right;
    };
    
    int sumOfLeftLeaves(struct TreeNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	int res = 0;
    	if (root->left != NULL && root->left->left == NULL && root->left->right == NULL)
    	{
    		res += root->left->val;
    	}
    	return res + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
    }
    
    
    /*主函数省略*/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    十【提交结果】

    1. Java语言版
      在这里插入图片描述

    2. C语言版
      在这里插入图片描述

  • 相关阅读:
    ChatGLM lora微调时出现KeyError: ‘context‘的解决方案
    MySQL优化01-索引
    SpringBoot集成Mybatis-plus
    数据库基本操作--DML
    旋转衬垫控制器 ( Rotation Shim Controller ) 是什么
    Linux之进程管理
    计网 | 【五 传输层、六 应用层】知识点及例题
    P02 反射
    C++进阶语法——OOP(面向对象)【学习笔记(四)】
    Effective C++条款20:宁以pass-by-reference-to-const替换pass-by-value
  • 原文地址:https://blog.csdn.net/IronmanJay/article/details/126001579