刷代码随想录有感(45):二叉树的最大深度

题干:

力扣这里给了定义:二叉树的最大深度指的是从根节点开始,到最远叶子所经过的节点数。

代码:

class Solution {//递归实现
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL)return NULL;
        int leftheight = maxDepth(root -> left);
        int rightheight = maxDepth(root -> right);
        int height = 1 + max(leftheight, rightheight);

        return height;
    }
};
class Solution {//迭代实现
public:
    int maxDepth(TreeNode* root) {
        int count = 0;
        queue<TreeNode*> que;
        vector<vector<int>> res;
        if(root != NULL) que.push(root);
        while(!que.empty()){
            int size = que.size();
            vector<int> tmp;
            while(size--){
                TreeNode* node = que.front();
                que.pop();
                tmp.push_back(node -> val);
                if(node -> left) que.push(node -> left);
                if(node -> right) que.push(node -> right);
                if(size == 0) count+=1;
            }
        }
        return count;

    }
};

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 14:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 14:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 14:42:02       87 阅读
  4. Python语言-面向对象

    2024-04-24 14:42:02       96 阅读

热门阅读

  1. 【无标题】远程链接服务实战

    2024-04-24 14:42:02       188 阅读
  2. 预防oracle的漏洞及其提权

    2024-04-24 14:42:02       34 阅读
  3. fps游戏断线重连架构设计

    2024-04-24 14:42:02       29 阅读
  4. 数据结构—单链表实现通讯录

    2024-04-24 14:42:02       32 阅读
  5. 算法第42天动态规划4

    2024-04-24 14:42:02       31 阅读
  6. 捷克营业执照申请

    2024-04-24 14:42:02       39 阅读
  7. office的文件(word、excel、ppt)图标变白

    2024-04-24 14:42:02       93 阅读
  8. websocket 和 eventsource 的区别和应用

    2024-04-24 14:42:02       33 阅读
  9. 解决常见的 `npm install` 报错

    2024-04-24 14:42:02       28 阅读
  10. Python的一些中级用法

    2024-04-24 14:42:02       45 阅读