104. Maximum Depth of Binary Tree

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)如果二叉树为空,二叉树的深度为0
(2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1

代码如下:

 /**
* Definition for a binary tree node.
* 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 == NULL)
{
return ;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right); return left > right ? (left + ) : (right + );
}
};

leetcode 104的更多相关文章

  1. 递归的三部解题曲 关联leetcode 104. 二叉树最大深度练习

    递归关心的三点 1. 递归的终止条件 2. 一级递归需要做什么 3. 返回给上一级递归的返回值是什么 递归三部曲 1. 找到递归的终止条件:递归什么时候结束 2. 本级递归做什么:在这级递归中应当完成 ...

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

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

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

  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. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

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

  6. leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree

    104: class Solution { public: int maxDepth(TreeNode* root) { if(root == NULL) ; int left = maxDepth( ...

  7. Java实现 LeetCode 104 二叉树的最大深度

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

  8. LeetCode 104. Maximum Depth of Binary Tree

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

  9. leetcode 104 Maximum Depth of Binary Tree ----- java

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

随机推荐

  1. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  2. Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.

    去配置文件中找 , 某个配置文件被引用了两次以上.移除后保留一个即可.如下即可产生上述问题 <import resource="classpath:testContext-curren ...

  3. webview和 内置浏览器的调用

    http://blog.csdn.net/hudashi/article/details/8176298/ 一.启动android默认浏览器 在Android程序中我们可以通过发送隐式Intent来启 ...

  4. (C) Windows 32 API程序设计目录

    (一)第一个窗口程序 01 创建第一个窗口. (二)输出文本 01 获取Windows图形构件大小信息

  5. angularJs的ui-router总结

    一:跑通ui-router. ui-router源码在最后面 跑通后的样子: 这个不解释了,都是很基本的东西. 二:切换视图: 这里的name可以不写,但是你得放到state的第一个参数里. 跑起来后 ...

  6. Javascript 原型编程初探

    创建两个对象独立运行 var PlayTrace = function (interVal,name) { this.interVal = interVal; this.playName = name ...

  7. C++primer 练习13.39

    13.39 编写你自己版本的StrVec,包括自己版本的reserve,capacity(参见9.4节,第318页)和resize(参见9.3.5节,第314页) 13.40 为你的StrVec类添加 ...

  8. 【原】linux系统运维工具必备

    操作系统:CentOS※,Ubuntu 网站服务:apache,nginx※,tomcat,tengine 开发语言:php,python※,shell※ 数据库 :Mysql※ 代理相关:lvs,k ...

  9. 初次体验架设PHP网站

     最近需要快速架设一个网站,因此淘了一份成型的模板,然后就开始..过程比较曲折. 测试环境:Win7旗舰+IIS7.5+mysql5.6+PHP5.2.17+PHPManagerForIIS-1.1. ...

  10. Object的toString决定了重写equals

    默认的toString结果public String toString(){    return getClass().getName()+"@"+Integer.toHexStr ...