day16打卡

day16打卡

104. 二叉树的最大深度

  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)
cpp 复制代码
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
cpp 复制代码
class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        int depth = 0;
        while(!q.empty())
        {
            int size = q.size();
            depth++;
            for(int i = 0 ; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
        }
        return depth;
    }
};

111. 二叉树的最小深度

  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)

注意最小深度即可。

cpp 复制代码
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        if(root->left == nullptr && root->right != nullptr)
        {
            return 1 + minDepth(root->right);
        }
        if(root->left != nullptr && root->right == nullptr)
        {
            return 1 + minDepth(root->left);
        }
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)

注意左右节点都为空时就是叶子节点,此时返回depth即可

cpp 复制代码
class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        int depth = 0;
        while(!q.empty())
        {
            int size = q.size();
            depth++;
            for(int i = 0 ; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
                if(top->left == nullptr && top->right == nullptr) return depth;
            }
        }
        return depth;
    }
};

222. 完全二叉树的节点个数

  • 递归法

  • 时间复杂度:O(N),空间复杂度:O(N)

cpp 复制代码
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == nullptr) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
cpp 复制代码
class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        int count = 0;
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0 ; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                count++;
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
        }
        return count;
    }
};

->left) q.push(top->left);

if(top->right) q.push(top->right);

}

}

return count;

}

};

复制代码
相关推荐
沐怡旸1 小时前
【算法】【链表】328.奇偶链表--通俗讲解
算法·面试
掘金安东尼4 小时前
Amazon Lambda + API Gateway 实战,无服务器架构入门
算法·架构
码流之上5 小时前
【一看就会一写就废 指间算法】设计电子表格 —— 哈希表、字符串处理
javascript·算法
快手技术7 小时前
快手提出端到端生成式搜索框架 OneSearch,让搜索“一步到位”!
算法
CoovallyAIHub1 天前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP1 天前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo1 天前
半开区间和开区间的两个二分模版
算法
moonlifesudo1 天前
300:最长递增子序列
算法
CoovallyAIHub1 天前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉