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;

}

};

复制代码
相关推荐
隔壁大炮6 分钟前
Day07-词嵌入层解释
人工智能·深度学习·算法·计算机视觉·cnn
啊我不会诶9 分钟前
Codeforces Round 1091 (Div. 2) and CodeCraft 26
c++·算法
凌波粒11 分钟前
LeetCode--二叉树前中后序遍历的递归与迭代实现(二叉树/DFS)
算法·leetcode·深度优先
啊哦呃咦唔鱼13 分钟前
Leetcodehot100-215. 数组中的第K个最大元素
数据结构·算法·leetcode
老赵聊算法、大模型备案18 分钟前
从剪映、即梦 AI 被罚,读懂 AI 生成内容标识硬性合规要求
人工智能·算法·安全·aigc
苏渡苇19 分钟前
Redis 核心数据结构(二)——List 与消息队列
数据结构·redis·list·redis发布订阅
shehuiyuelaiyuehao28 分钟前
算法12,滑动窗口,将x减到0的最小操作数
java·数据结构·算法
6Hzlia30 分钟前
【Hot 100 刷题计划】 LeetCode 19. 删除链表的倒数第 N 个结点 | C++ 双指针单趟遍历
c++·leetcode·链表
_深海凉_33 分钟前
LeetCode热题100-跳跃游戏 II
算法·leetcode·游戏
csuzhucong36 分钟前
力扣OJ(2301-2600)
算法·leetcode·职场和发展