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.

此题是经典的求二叉树深度深度题目,可采用递归的方式,以此求每个节点的左子树深度和右子树深度,然后返回该节点左右子树深度最大的那个即为该节点的深度

具体代码如下:

 /**
* 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 == NULL)
return ; int nleft = maxDepth(root->left);
int nright = maxDepth(root->right); return (nleft > nright) ? (nleft+) : (nright+);
}
};

Maximum Depth of Binary Tree 二叉树的深度的更多相关文章

  1. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

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

  6. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

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

  8. [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度

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

  9. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

随机推荐

  1. JSP中使用JSTL表达式

    最近写web项目,为了使JSP代码美观好维护,决定采用EL&JSTL表达式.    EL表达式直接就可以用,但是JSTL表达式是属于apache的一个开源库,这个用起来就需要倒入一些jar包之 ...

  2. JavaScript《一》

    脚本语言概念:不需要提前编译的,即时执行的语言,如js,t-sql等 在一个js块中,只要有一个语句出现错误,整个块都不执行 强类型:在编译时就已经确定的类型,弱类型,在运行时,编译器自动根据赋值在确 ...

  3. c# 操作excle[转]

    //引用Microsoft.Office.Interop.Excel.dll文件 //添加using using Microsoft.Office.Interop.Excel; using Excel ...

  4. Mac 10.12安装Homebrew图形化界面管理工具Cakebrew

    下载: (链接: https://pan.baidu.com/s/1mivJ9H2 密码: f8dr)

  5. getActionBar()为null的解决方法总结(引用他人)

    最近在看android actionBar的使用,环境为AndroidStudio,建一个简单的工程,功能为:两个按钮,一个单击用于显示actionbar,一个用于隐藏actionbar.默认acti ...

  6. security和oauth2.0的整合

    security和oauth2.0的整合 之前已经介绍过security的相关的介绍,现在所需要做的就是security和oauth2.0的整合,在原有的基础上我们加上一些相关的代码;代码实现如下: ...

  7. PHP 浮点数 转化 整数方法对比 ceil,floor,round,intval,number_format

    ceil,floor,round,intval,number_format - 执行1000W此效率对比 Header("Content-Type:text/html;charset=utf ...

  8. JavaScript设计模式-5.接口

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Tomcat8-启动脚本分析

    1. Tomcat也是一个java程序 最终的入口启动文件:org.apache.catalina.startup.Bootstrap 最后一条命令: start "Tomcat" ...

  10. 利用 ICEpdf 快速实现 pdf 文件预览功能

    之前工作中,需要实现一个在线预览pdf的功能,一开始用的的 jQuery-media 插件来实现的,后来感觉有点慢,就继续寻找更好的替代品,直到遇见了 ICE pdf... ICEpdf (官网:ht ...