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 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 ;
_max = ;
tranverse(root->left, );
tranverse(root->right, );
return _max;
}
void tranverse(TreeNode * node, int count)
{
if(node == NULL)
return;
else{
if(count + > _max)
_max = count + ;
tranverse(node->left, count + );
tranverse(node->right, count + );
}
}
public:
int _max;
};

java版的如下所示,上面的递归写的还是太麻烦了,下面的简单很多哈:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
return tranverse(root, 0);
} public int tranverse(TreeNode root, int val){
if(root.left == null && root.right == null)
return val+1;
int leftDep = 0, rightDep = 0;
if(root.left != null)
leftDep = tranverse(root.left, val + 1);
if(root.right != null)
rightDep = tranverse(root.right, val + 1);
return Math.max(leftDep, rightDep);
}
}

LeetCode OJ: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. [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二叉树的最大深度 C++/Java

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

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. Leetcode 104 Maximum Depth of Binary Tree 二叉树

    计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...

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

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

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

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

  10. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. Java多线程(Java总结篇)

    Java总结篇:Java多线程 多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上 ...

  2. MQ,互联网架构解耦神器

    一个架构常识:当调用方需要关心执行结果,通常使用RPC调用. ret = PassportService::userAuth(name, pass); switch(ret){  case(YES) ...

  3. Oracle索引表

    索引组织表(Index-Organized Table)是按B-树的结构来组织和存储数据的.与标准表中的数据时无序存放的不同,索引表中数据按主键值有序存储. 叶子节点中存放的是表的主键值与所有非主键值 ...

  4. 03_Hadoop简单介绍以及版本信息

    一.海量数据: 量:大.数目多,数据量到达PB.ZB级别,条目数到达几十亿条.百亿条 1)存储:分布式,集群的概念,管理(主节点.从节点),HDFS(HadoopDistributedFileSyst ...

  5. Vuex的入门教程

    前言 在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式,详细点击这篇文章查看. 但是如果是大型项目,很多时候都需要在子组件之间传递 ...

  6. WEB-INF有关的目录路径总结、转向方式: forward 重定向方式: Redirect

    WEB-INF有关的目录路径总结 1.资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了. 2.页面放在WEB-INF目录下面,这样可以限制访问,提 ...

  7. Dual Boot WINDOWS 10 and KALI LINUX Easily STEP BY STEP GUIDE截图

    mark. kali安装:https://www.youtube.com/watch?v=KLj2yQPWZDk 删除无用分区:http://www.xitongcheng.com/jiaocheng ...

  8. 20145239杜文超《网络对抗》- Web安全基础实践

    20145239杜文超<网络对抗>- Web安全基础实践 基础问题回答 (1)SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查 ...

  9. Win32 API编程:WinMain无法重载函数或_tWinMain无法重载

    #include "windows.h" #include "tchar.h" int APIENTRY _tWinMain( HINSTANCE hInsta ...

  10. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...