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;

}

};

复制代码
相关推荐
zxsz_com_cn6 分钟前
设备预测性维护系统实战指南:架构、算法与落地路径
算法·架构
爪哇部落算法小助手11 分钟前
爪哇周赛 Round 3
数据结构·c++·算法
吃着火锅x唱着歌25 分钟前
LeetCode 3623.统计梯形的数目 I
算法·leetcode·职场和发展
迷途之人不知返26 分钟前
二叉树的链式结构
数据结构
不会c嘎嘎32 分钟前
【数据结构】红黑树详解:从原理到C++实现
开发语言·数据结构
吃着火锅x唱着歌38 分钟前
LeetCode 2364.统计坏数对的数目
数据结构·算法·leetcode
qq_3363139342 分钟前
java基础-set类集合进阶
java·算法
kyle~1 小时前
数据结构---堆(Heap)
服务器·开发语言·数据结构·c++
嵌入式老牛1 小时前
第13章 图像处理之Harris角点检测算法(二)
图像处理·opencv·算法·计算机视觉
渡我白衣1 小时前
哈希的暴力美学——std::unordered_map 的底层风暴、扩容黑盒与哈希冲突终极博弈
java·c语言·c++·人工智能·深度学习·算法·哈希算法