题目描述

求给定二叉树的最大深度,
最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.


示例1

输入

复制

{1,2}

输出

复制

2
示例2

输入

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     *
     * @param root TreeNode类
     * @return int整型
     */
    int maxDepth(TreeNode* root) {
        // write code here
        if (root==NULL){return 0;}
        queue<TreeNode*> que;
        que.push(root);
        int depth=0;
        while (!que.empty()){
            int size=que.size();
            depth++;
            for (int i=0;i<size;++i){
                TreeNode *tmp=que.front();
                que.pop();
                if (tmp->left !=NULL)
                    que.push(tmp->left);
                if (tmp->right !=NULL)
                    que.push(tmp->right);
            }
        }
        return depth;
    }
};

leetcode45:maximum depth of binary tree的更多相关文章

  1. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  2. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  3. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  4. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  7. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  8. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

随机推荐

  1. 笔记本键盘按U键却变成了4

    解答 笔记本键盘U盘变成了4,是因为你开启了小键盘功能.出现该问题,只要关闭小键盘功能即可,操作如下: 按住键盘下方的Fn,同时按住键盘顶部的F键中标有Numlk的键. 电脑屏幕出现解锁标志,小键盘功 ...

  2. HTTPS证书知识扫盲

    1. 前言 现在搞网站域名不加个HTTPS就显得不专业,特别在使用JWT进行认证的接口一定要加HTTPS为你的接口增加一层安全屏障.今天就来聊聊配置HTTPS的关键SSL证书,也被称为CA证书. 2. ...

  3. 【8】进大厂必须掌握的面试题-Java面试-异常和线程

    Q1.错误和异常有什么区别? 错误是在运行时发生的不可恢复的情况.如OutOfMemory错误.这些JVM错误无法在运行时修复.尽管可以在catch块中捕获错误,但是应用程序的执行将停止并且无法恢复. ...

  4. python post与get请求的区别

    post:请求的url不带参数               查询参数在WebForms保存 get:请求的url会附带查询参数       查询参数在QueryString保存

  5. zookeeper的管理功能

    一,查看当前zookeeper的版本 [liuhongdi@localhost ~]$ echo stat|nc 127.0.0.1 2181 Zookeeper version: 3.5.6-c11 ...

  6. 没事学学KVM(二)创建一台虚拟机

    首先通过VMware创建一台虚机,建议内存大于1G,并开启CPU 的inter vt-x功能,安装好对应的软件后,yum install -y qemu-kvm* virt-* libvirt* 准备 ...

  7. ORA-12609报错分析

    问题:监控不断告警ORA-12609 Wed 10/14/2020 10:40 AM 12CRAC1-ALERT中出现ORA错误,请检查 171- nt OS err code: 0 172- Cli ...

  8. javascript常见面试题之一:数组的冒泡排序;

    var arr=[32,2,7,78,90,10]; //外层循环控制轮数: for (var i = 0; i < arr.length; i++) { //内层循环控制次数: for (va ...

  9. 并发编程——多线程计数的更优解:LongAdder原理分析

    前言 最近在学习ConcurrentHashMap的源码,发现它采用了一种比较独特的方式对map中的元素数量进行统计,自然是要好好研究一下其原理思想,同时也能更好地理解ConcurrentHashMa ...

  10. Kubernetes Controller详解

    运行容器化应用是Kubernetes最重要的核心功能.为满足不同的业务需要,Kubernetes提供了多种Controller,主要包括Deployment.DaemonSet.Job.CronJob ...