题目描述

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

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. Matlab2016b安装流程

    来源:https://jingyan.baidu.com/article/59703552da12ab8fc007402b.html Matlab2016b安装教程 听语音 原创 | 浏览:34338 ...

  2. C语言中time_t数据类型详细介绍

    包含文件:<time.h> #ifndef __TIME_T #define __TIME_T     /* 避免重复定义 time_t */ typedef long     time_ ...

  3. 启动VNC Shell扩展

    下载source files - 18.3 Kb Introduction 我们使用RealVNC来远程控制我们的网络中的pc机,VNC是一个伟大的产品,但如果不记住计算机名称,它可以是乏味的,在网络 ...

  4. 如何彻底杀掉Oracle相关用户的会话

    在ORACLE数据库当中,有时候会使用ALTER SYSTEM KILL SESSION 'sid,serial#'杀掉一个会话进程,但是使用这个SQL语句杀掉会话后,数据库并不会立即释放掉相关的资源 ...

  5. linux系统上用户态pppoe收发包过程

    花了几天看了一下ppp/pppoe有关的东西,画了一下用户态pppoe收发包的示意图.

  6. 2016年 实验四  B2B模拟实验

    实验四  B2B模拟实验 [实验目的] ⑴.掌握B2B中供应商的供求信息发布.阿里商铺开设和订单交易等过程. ⑵.掌握B2B中采购商的采购信息的发布.交易洽谈.网上支付和收货等过程. [实验条件] ⑴ ...

  7. 实验五  用PS制作图文合成海报

    实验五  用PS制作图文合成海报 [实验目的] ⑴.熟悉PS软件基本操作 ⑵.学会用PS制作内容较丰富的海报式广告 [实验条件] ⑴.个人计算机一台 ⑵.个人计算机中预装Windows7操作系统和浏览 ...

  8. 从源码的角度解析Mybatis的会话机制

    坐在我旁边的钟同学听说我精通Mybatis源码(我就想不通,是谁透漏了风声),就顺带问了我一个问题:在同一个方法中,Mybatis多次请求数据库,是否要创建多个SqlSession会话? 可能最近撸多 ...

  9. 【LWJGL3】LWJGL3的内存分配设计,第一篇,栈上分配

    简介 LWJGL (Lightweight Java Game Library 3),是一个支持OpenGL,OpenAl,Opengl ES,Vulkan等的Java绑定库.<我的世界> ...

  10. ScanTailor-ScanTailor 自动矫正图像歪斜

    ScanTailor 自动矫正图像歪斜   下面操作某一步可能会卡住,别担心情等待它处理完毕.   New Project ... Input Directory "Browse" ...