给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离。

您在真实的面试中是否遇到过这个题?

Yes
样例

给出一棵如下的二叉树:

  1
/ \
2 3
/ \
4 5

这个二叉树的最大深度为3.

思路:与二叉树最小深度思路一样,一次AC;

这种容易题目要很熟练,主要是思路要清晰。

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
/*
思路:与二叉树最小深度思路一样,一次AC;
这种容易题目要很熟练。
*/
int maxDepth(TreeNode *root) {
// write your code here if(root==NULL){
return 0;
} if(root->left==NULL){
return maxDepth(root->right)+1;
} if(root->right==NULL){
return maxDepth(root->left)+1;
} return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};

Lintcode---二叉树的最大深度的更多相关文章

  1. lintcode :二叉树的最大深度

    题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3. 解 ...

  2. 【Lintcode】二叉树的最大深度 - 比较简单,用递归比较好,不递归也能做,比较麻烦

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的 ...

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

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

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

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

  5. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  6. LeetCode(104):二叉树的最大深度

    Easy! 题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null, ...

  7. [Leetcode] Maximum depth of binary tree二叉树的最大深度

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

  8. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  9. LeetCode初级算法--树01:二叉树的最大深度

    LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

  10. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

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

随机推荐

  1. Problem C: 矩阵对角线求和

    #include<stdio.h> int main() { ][]; scanf("%d",&n); ,sum2=; ;i<n;i++) ;j<n ...

  2. VUE2.0学习总结

    摘要: 年后公司项目开始上vue2.0,自己对学习进行了总结,希望对大家有帮助! VUE2.0学习 vue介绍 vue是什么? https://vuefe.cn/guide vue也是一个数据驱动框架 ...

  3. SQL locate()函数

    LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回字符串str第一次出现的子串substr的位置. 第二个语法返回第一次出现在字符串str的子串sub ...

  4. MyEclipse安装后需要进行的配置

    摘自: http://hi.baidu.com/timesten/item/c826983a6f9654ffde2221c0 MyEclipse安装后需要进行的配置 在MyEclipse中编写Web ...

  5. dbus启动失败:Couldn't connect to system bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

    在没有开启x窗口的shell下启动dbus相关程序时会如上错误,详细原因如下: This is not considered to be a bug. Auto-launching D-Bus ses ...

  6. 爪哇国新游记之一----第一个类Cube

    将这个类作为Java学习的第一个类,简单易懂易上手. /** * 正方体类 */ public class Cube { private int length;// 正方体边长 private sta ...

  7. PortableApps的使用方法

    1 从官方网站下载这个软件,建议只下载PortableApps Platform Only即可,因为官方提供的软件其实很少,大多数需要我们自己添加. PortableApps 致力于将一些常见的开源软 ...

  8. vue - for遍历数组

    注释上,也很清楚了哈. 1. item是循环名字,items是循环的数组 <!DOCTYPE html> <html lang="en"> <head ...

  9. 在k8s中的基本概念

    在k8s中的基本概念 一.Pod1. podk8s下最重要也最基本的概念,由一个根容器Pause和许多用户业务容器组成,是容器的载体. 2. pod的yaml定义格式及字段 apiVersion: v ...

  10. 经常使用meta标签属性

    <meta> 1.Keywords (keyword) 说明:告诉搜索引擎你网页的keyword是什么. 使用方法:<meta name="keywords" c ...