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.

递归解题思路:

①结点为空时,返回0;②当前结点的深度 = max(两个孩子结点各自最大深度)+ 1

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if (!root)
return ; return max(maxDepth(root->left),
maxDepth(root->right)) + ;
}
};

迭代的解法:

用queue先进先出的特性,按层遍历,最后返回遍历的层数。

 class Solution {
public:
int maxDepth(TreeNode *root) {
if (!root) {
return ;
} queue<TreeNode *> nodeQue;
nodeQue.push(root);
int levelNodesCnt;
int depth = ; while (!nodeQue.empty()) {
depth++;
levelNodesCnt = nodeQue.size();
while (levelNodesCnt--) {
if (nodeQue.front()->left)
nodeQue.push(nodeQue.front()->left);
if (nodeQue.front()->right)
nodeQue.push(nodeQue.front()->right);
nodeQue.pop();
}
} return depth;
}
};

附录:

C++ queue使用

【Leetcode】【Easy】Maximum Depth of Binary Tree的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  2. 【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...

  3. 【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 ...

  4. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

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

  5. 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 ...

  6. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  7. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  8. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. sharepoint_study_5

    描述:手动进行SharePoint网页调试图解 解决: 第一步:打开页面的后台代码,设置断点 第二步:添加到进程 第三步:选择SharePoint进程,我这里都选了,如果你知道要调试的页面是哪一个进程 ...

  2. 关于cin 与 cout 的加速

    在用cin 与 cout 的时候 可以使用 ios::sync_with_stdio(); cin.tie(); cout.tie(); 这样在输入大数据的时候可以加快许多

  3. indexOf获取字符位置

    先定义一个字符串: var aString = "you are beautiful,so beautiful,and i love you ver much"; 拿到第一个逗号的 ...

  4. PIE SDK元素的删除

    1功能简介 元素删除是将根据需求将不符合的元素进行删除,PIE SDK支持元素的删除操作,下面对元素的删除功能进行介绍. 2功能实现说明 2.1.1 实现思路及原理说明 第一步 获取已经选择的元素 第 ...

  5. zabbix 另一种方式取 zabbix-sender

    一,zabbix-sender介绍 这种模式是两主机并没有agent互联 使用zabbix-serder的话适用那种没有固定公网IP的,实时系统数据监控操作 还一个实用为零延迟数据监控, 本省zabb ...

  6. Python 的 __new__()方法与实例化

    __new__() 是新式类中才有的方法,它执行在构造方法创建实例之前.可以这么理解,在 Python 中类中的构造方法 __init__() 负责将类实例化,而在 __init__() 启动之前,_ ...

  7. jdk7.NIO.2学习笔记之目录文件及权限

    package com.zl.jdk7; import java.io.File; import java.io.IOException; import java.nio.file.Path; imp ...

  8. 使用SeaJS实现模块化JavaScript开发【转】

    前言 SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.与jQuery等JavaScript框架不同,SeaJS不会扩展封 ...

  9. Windows 10 下彻底关闭 Hyper-V 服务

    由于最近需要用到VMWare Workstation 安装虚拟机,安装完成后,发现任何64位的系统都不能正常安装.可能是Hyper-V与VMWare Workstation的冲突造成的不兼容,所以就去 ...

  10. Java学习第二十二天

    1:登录注册IO版本案例(掌握) 要求,对着写一遍. cn.itcast.pojo User cn.itcast.dao UserDao cn.itcast.dao.impl UserDaoImpl( ...