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. curl命令使用小结[转]

    curl命令有不少小技巧,linux下测试确实很方便.转http://blog.csdn.net/zhangliang_571/article/details/26379777 1.开启gzip请求c ...

  2. Laravel5 cookie和session设置

    Cookies Laravel会加密所有已创建的cookie信息,并附加上授权码,当客户端擅自修改cookie信息时,该cookie将被废弃,从而保证安全性. 获取一个指定的cookie值 $valu ...

  3. Linux中记录终端(Terminal)输出到文本文件(转载)

    一,如何把命令运行的结果保存到文件当中? 这个问题太简单了,大家都知道,用 > 把输出转向就可以了 例子: [lhd@hongdi ~]$ ls > ls.txt [lhd@hongdi ...

  4. Java多线程之DaemonThreadFactory

    通过DaemonThreadFactory创建后台线程池 另外:如果是后台线程创建的线程,将都是后台线程. package wzh.daemon; import java.util.concurren ...

  5. oracle查看数据库的字符集

    注意如果是从旧的数据库复制,一定要保证字符集使用一模一样的,不然会有很多问题(比如汉字在UTF8占3个字符,在GBK占2个字符,所以设置的列宽度要比原来的大才行,不然就会报值太大的错误) select ...

  6. C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭

    迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...

  7. IE10修改select样式

    一般我们针对webkit内核的,都使用-webkit-appearance来控制一些表单元素. 但是IE10如何来控制呢? select::-ms-expand{display:none;}

  8. Fedora 14配置vsftp服务步骤

    Fedora 14配置vsftp服务步骤:1:检查Fedora14是否安装了vsftp服务    用rpm -qa|grep vsftp命令检查是否安装了vsftp服务,如果安装了,会显示安装好的版本 ...

  9. Js 时间与字符串转示例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. Bootstrap栅格系统

    栅格系统分为两种:默认栅格系统 row,流式栅格系统 row-fluid. row 默认栅格系统:即指定了每个栅格的宽度为60px,间距为20px.共有12个栅格.总宽度为940px; 即12个栅格= ...