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. 并发编程>>线程池的实现(四)

    线程创建倾向 如果运行的线程的小于corePoolSize,当请求来时,创建新线程. 如果运行corePoolSize或多于,当请求来时,排队. 如果请求不能进行排队,且小于maximumPoolSi ...

  2. 2018青岛网络赛G - Couleur 区间上的启发式合并

    题意:给出\(a[1...n]\),共\(n\)次操作,每次删除一个位置\(p_i\)(强制在线),此时区间会变为两个分离的区间,求每次操作的最大区间逆序对 首先要知道必要的工具,按权值建立的主席树可 ...

  3. 基于iTop4412的FM收音机系统设计(三)

    说明:第一版架构为:APP+JNI(NDK)+Driver(linux),优点是开发简单,周期短,也作为自己的毕业设计 现在更新第二版,FM服务完全植入Android系统中,成为系统服务,架构为:AP ...

  4. 微信内置的浏览器window.location.href 跳转不兼容问题

    1.不兼容苹果手机---->>>>使用模拟触发a标签 <a id="alink" href="http://www.baidu.com&qu ...

  5. Mac下的裁剪快捷键

    按[Command]+[C]复制文件,然后按[Command]+[Option]+[V]. 注意:[Command]+[X]只能剪切文字文本.

  6. Linux网络编程服务器模型选择之IO复用循环并发服务器

    在前面我们介绍了循环服务器,并发服务器模型.简单的循环服务器每次只能处理一个请求,即处理的请求是串行的,效率过低:并发服务器可以通过创建多个进程或者是线程来并发的处理多个请求.但是当客户端增加时,就需 ...

  7. Python中utf-8与utf-8-sig两种编码格式的区别

    As UTF-8 is an 8-bit encoding no BOM is required and anyU+FEFF character in the decoded Unicode stri ...

  8. Cloudera Manager安装之Cloudera Manager安装前准备(CentOS6.5)(一)

    Cloudera Manager安装前准备 (一)机器准备 192.168.80.148   clouderamanager01 (部署ClouderaManager-server和Mirror se ...

  9. PHP之string之chr()函数使用

    chr (PHP 4, PHP 5, PHP 7) chr - Return a specific character chr - 返回指定的字符 Description string chr ( i ...

  10. JDK源码--ArrayList浅析

    先上别人的源码分析http://www.cnblogs.com/roucheng/p/jdkfenxi.html 这个链接也不错:http://www.jianshu.com/p/8d14b55fa1 ...