• 111. 二叉树的最小深度


    题目链接:

    力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

    我的想法:

            递归肯定能做,但是我第一想法是层序遍历,层序遍历兼职万金油。层序遍历,遇到节点的左右都空就表示最小了。

    层序遍历法:

    我的代码:

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. int minDepth(TreeNode* root) {
    15. if(root == nullptr) return 0;
    16. std::queue que;
    17. que.push(root);
    18. int count = 0;
    19. while(!que.empty())
    20. {
    21. int size = que.size();
    22. count++;
    23. for(int i = 0; i < size; i++)
    24. {
    25. TreeNode* node = que.front();
    26. que.pop();
    27. if(node->left==nullptr && node->right==nullptr)
    28. {
    29. return count;
    30. }
    31. if(node->left) que.push(node->left);
    32. if(node->right) que.push(node->right);
    33. }
    34. }
    35. return count;
    36. }
    37. };

    递归法:

            三部曲:  

            1.int mdepth(Tnode* cur);

            2.if(cur->left==null && cur->right==null) return 0;

            3.int l = mdepth(cur->left);

               int r = mdepth(cur->right);

                return 1 + min(l, r);

    自己写的错误代码:

    1. class Solution {
    2. public:
    3. int minDepth(TreeNode* root) {
    4. if(root == nullptr) return 0;
    5. return mdepth(root);
    6. }
    7. int mdepth(TreeNode* cur)
    8. {
    9. if(cur->left==nullptr && cur->right==nullptr) return 0;
    10. int l,r;
    11. if(cur->left) l = mdepth(cur->left);
    12. if(cur->right) r = mdepth(cur->right);
    13. return 1 + min(l,r);
    14. }
    15. };

    正确代码:

    1. class Solution {
    2. public:
    3. int getDepth(TreeNode* node) {
    4. if (node == NULL) return 0;
    5. int leftDepth = getDepth(node->left); // 左
    6. int rightDepth = getDepth(node->right); // 右
    7. // 中
    8. // 当一个左子树为空,右不为空,这时并不是最低点
    9. if (node->left == NULL && node->right != NULL) {
    10. return 1 + rightDepth;
    11. }
    12. // 当一个右子树为空,左不为空,这时并不是最低点
    13. if (node->left != NULL && node->right == NULL) {
    14. return 1 + leftDepth;
    15. }
    16. int result = 1 + min(leftDepth, rightDepth);
    17. return result;
    18. }
    19. int minDepth(TreeNode* root) {
    20. return getDepth(root);
    21. }
    22. };

    先序的递归更好理解:

    1. class Solution {
    2. private:
    3. int result;
    4. void getdepth(TreeNode* node, int depth) {
    5. // 函数递归终止条件
    6. if (root == nullptr) {
    7. return;
    8. }
    9. // 中,处理逻辑:判断是不是叶子结点
    10. if (root -> left == nullptr && root->right == nullptr) {
    11. res = min(res, depth);
    12. }
    13. if (node->left) { // 左
    14. getdepth(node->left, depth + 1);
    15. }
    16. if (node->right) { // 右
    17. getdepth(node->right, depth + 1);
    18. }
    19. return ;
    20. }
    21. public:
    22. int minDepth(TreeNode* root) {
    23. if (root == nullptr) {
    24. return 0;
    25. }
    26. result = INT_MAX;
    27. getdepth(root, 1);
    28. return result;
    29. }
    30. };

  • 相关阅读:
    volatility, polarizability, viscosity, electronegativity & hydrogen bonding
    细说GaussDB(DWS)复杂多样的资源负载管理手段
    深度学习:VGG(Vision Geometrical Group)论文详细讲解
    Fedora 35 编译安装ffmpeg 5.1 —— 筑梦之路
    centos7安装Nginx
    使用cgroup控制内存
    如何快速掌握DDT数据驱动测试?
    io多路复用之poll的详细执行过程
    Docker容器-------数据卷和数据卷容器及cgroups资源限制
    【七夕快乐篇】Nacos是如何实现服务注册功能的?
  • 原文地址:https://blog.csdn.net/weixin_44965579/article/details/132744275