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

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

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

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. Activity(活动)生命周期--项目测试

    一.新建一个Activitylifetest项目,允许Android Studio帮我们自动创建活动和布局,并且勾选Luancher Activity来将创建的活动设置为主活动. 可以参考: http ...

  2. ES6 Set结构和Map结构(上)

    Set ES6提供了新的数据结构--Set,它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身也是一个构造函数,用来生成Set数据结构 var s = new Set(); [2,3,5 ...

  3. Java高级架构师(一)第35节:Nginx的Location区段

    没有修饰符 表示:必须以指定模式开始. 表示/abc下的所有内容都可以被访问. = 表示与指定的模式精确匹配,可以带参数. 实例中要求区分大小写,并以c结尾. 实例中指定的正则表达式不区分大小写. 注 ...

  4. httpWebRequest 文件下载

    服务版本: go file system ssdb github: https://github.com/dtxlink/gfs 上一篇: 一个 go 文件服务器 ssdb 通过 httpWebReq ...

  5. php-scandir()报错

    l       linux下 vim /usr/local/php/etc/php.in l       直接斜杠找 /disable_functions   回车 l       按i键 l     ...

  6. ms_sql_server_architecture

    We have classified the architecture of SQL Server into the following parts for easy understanding − ...

  7. http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html

    http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html http://blog.sina.com.cn/s/blog_538 ...

  8. VMware(bridge、NAT、host-only、custom)含义

    摘自: http://www.liangston.com/?post=48 VMware(bridge.NAT.host-only.custom)含义 作者:LiangSton 发布于:2012-1- ...

  9. Python自然语言处理资料库

    1.LTP [1]- 语言技术平台(LTP) 提供包括中文分词.词性标注.命名实体识别.依存句法分析.语义角色标注等丰富. 高效.精准的自然语言处理技术.经过哈工大社会计算与信息检索研究中心 11 年 ...

  10. 【云计算】OpenStack项目全面介绍

    关于OpenStack孵化项目trove(DBaaS)之我见:http://blog.csdn.net/ddl007/article/details/17253751 OpenStack Trove将 ...