day16打卡

day16打卡

104. 二叉树的最大深度

  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)
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)
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)

注意最小深度即可。

image-20240125223453615

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即可

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)

class Solution {
   
public:
    int countNodes(TreeNode* root) {
   
        if(root == nullptr) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
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. day13

    2024-01-26 16:12:04       40 阅读
  2. 算法day18

    2024-01-26 16:12:04       17 阅读
  3. 八股文day16——计算机网络(16

    2024-01-26 16:12:04       33 阅读
  4. mysql学习day17

    2024-01-26 16:12:04       33 阅读
  5. mysql学习day19

    2024-01-26 16:12:04       30 阅读
  6. 八股文day34——数据库(11

    2024-01-26 16:12:04       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-26 16:12:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-26 16:12:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-26 16:12:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-26 16:12:04       18 阅读

热门阅读

  1. 全网最全 MySQL EXPLAIN 完全解读

    2024-01-26 16:12:04       29 阅读
  2. Spring和 Springboot的区别你了解吗

    2024-01-26 16:12:04       36 阅读
  3. vue3基础

    2024-01-26 16:12:04       22 阅读
  4. springboot中使用easyExcel读取excel中的内容

    2024-01-26 16:12:04       30 阅读
  5. sql server 查询所有表的记录条数

    2024-01-26 16:12:04       36 阅读