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)
相关推荐
weixin_5835102839 分钟前
opencv图像处理框架
人工智能·深度学习·神经网络·算法·机器学习
程序员东min2 小时前
C++:抽象类习题
开发语言·c++
Victoria.a4 小时前
string类详解
数据结构·c++
转调6 小时前
第九章:内存池的调整与测试
c++·内存池
XY_墨莲伊7 小时前
【算法设计与分析】实验5:贪心算法—装载及背包问题
c语言·数据结构·c++·算法·贪心算法·排序算法
Happy_Traveller7 小时前
三角形的最大周长(976)
数据结构·算法·leetcode
水蓝烟雨7 小时前
[HOT 100] 2824. 统计和小于目标的下标对数目
算法·hot 100
KuaCpp7 小时前
搜索与图论复习2最短路
c++·算法·图论
weixin_307779138 小时前
Python获取能唯一确定一棵给定的树的最少数量的拓扑序列
数据结构·python
Lenyiin8 小时前
《 C++ 点滴漫谈: 二十五 》空指针,隐秘而危险的杀手:程序崩溃的真凶就在你眼前!
c++·nullptr·lenyiin·c++关键字