LeetCode 404.左叶子之和

LeetCode 404.左叶子之和

1、题目

题目链接: 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

2、深度优先搜索(递归)

思路

代码

cpp 复制代码
#include <iostream>

using namespace std;

//Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        // 如果根节点为空,则返回0
        if (root == nullptr) {
            return 0;
        }
        // 如果根节点既没有左子节点也没有右子节点(即该节点为叶子节点),返回0
        if (root->left == nullptr && root->right == 0) {
            return 0;
        }
        // 递归计算左子树的左叶子节点之和
        int leftValue = sumOfLeftLeaves(root->left);
        // 如果左子节点存在且为叶子节点,则将左子节点的值赋给leftValue
        if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
            leftValue = root->left->val;
        }
        // 递归计算右子树的左叶子节点之和
        int rightValue = sumOfLeftLeaves(root->right);
        // 返回左子树的左叶子节点之和加上右子树的左叶子节点之和
        return leftValue + rightValue;
    }
};

int main() {
    TreeNode* root = new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));
    Solution s;
    cout << s.sumOfLeftLeaves(root) << endl;
    return 0;
}

复杂度分析

  • 时间复杂度: O(n)
  • 空间复杂度: O(n)

3、深度优先搜索(精简版)

思路

代码

cpp 复制代码
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        // 如果根节点为空,则返回0
        if (root == nullptr) {
            return 0;
        }
        int leftValue = 0;
        // 如果根节点的左子节点不为空,且左子节点为叶子节点(即左子节点的左右子节点都为空)
        if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
            // 将左子节点的值赋给leftValue
            leftValue = root->left->val;
        }
        // 返回左子节点的值加上左子树和右子树中左叶子节点值的总和
        return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
    }
};

复杂度分析

  • 时间复杂度: O(n)
  • 空间复杂度: O(n)

4、广度优先搜索(迭代法)

思路

代码

cpp 复制代码
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        stack<TreeNode*> stk;
        if (root == nullptr) {
            return 0;
        }
        stk.push(root);
        int result = 0;
        while (!stk.empty()) {
            // 取出栈顶节点
            TreeNode* node = stk.top();
            stk.pop();
            // 判断左子节点是否存在,且为叶子节点
            if (node->left != nullptr && node->left->left == nullptr && node->left->right == nullptr) {
                // 如果是,则累加左子节点的值
                result += node->left->val;
            }
            // 如果右子节点存在,则入栈
            if (node->right) {
                stk.push(node->right);
            }
            // 如果左子节点存在,则入栈
            if (node->left) {
                stk.push(node->left);
            }
        }
        return result;
    }
};

复杂度分析

  • 时间复杂度: O(n)
  • 空间复杂度: O(n)
相关推荐
呼啦啦啦啦啦啦啦啦5 小时前
常见的排序算法
java·算法·排序算法
胡萝卜3.06 小时前
数据结构初阶:排序算法(一)插入排序、选择排序
数据结构·笔记·学习·算法·排序算法·学习方法
地平线开发者6 小时前
LLM 中 token 简介与 bert 实操解读
算法·自动驾驶
lyx33136967596 小时前
Pandas数据结构详解Series与DataFrame
数据结构·pandas
scx201310046 小时前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~6 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
散1127 小时前
01数据结构-交换排序
数据结构·算法
yzx9910137 小时前
Yolov模型的演变
人工智能·算法·yolo
weixin_307779138 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
无聊的小坏坏8 小时前
拓扑排序详解:从力扣 207 题看有向图环检测
算法·leetcode·图论·拓扑学