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.

解题:

递归就行:根节点为空,返回0;一个子树根节点为空,另一个子树根节点不为空,就返回根节点不为空的子树高度;否则返回两个子树中高度大者加一。

代码:

 /**
* 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 ;
if(root->left != NULL && root->right == NULL)
return maxDepth(root->left)+;
if(root->right != NULL && root->left == NULL)
return maxDepth(root->right)+;
int h_left = maxDepth(root->left);
int h_right = maxDepth(root->right);
return h_left > h_right ? h_left+:h_right+;
}
};

Java版本代码:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
return maxDepthHelper(root, 0);
}
int maxDepthHelper(TreeNode root,int height){
if(root == null)
return height;
int l = maxDepthHelper(root.left, height+1);
int r = maxDepthHelper(root.right, height+1);
return l > r?l:r;
}
}

【leetnode刷题笔记】Maximum Depth of binary tree的更多相关文章

  1. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  2. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

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

  4. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  7. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  8. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  9. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  10. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. smali语法高亮相关链接

    http://ruby-china.org/topics/8307 http://www.daqianduan.com/4820.html http://www.cnblogs.com/ruochen ...

  2. Android下ListView的分页(9.6)

    1 http://www.cnblogs.com/noTice520/archive/2012/02/10/2345057.html 2 http://www.92coding.com/blog/in ...

  3. ESXi安装iso镜像添加驱动(esxi6.5,6.7)

    准备工作:1.安装 Windows PowerShell 3.0 (需要启用Windows AutoUpdate服务,安装完毕计算机需要重启) https://www.microsoft.com/en ...

  4. WCF实现上传图片功能

    初次学习实现WCF winform程序的通信,主要功能是实现图片的传输. 下面是实现步骤: 第一步: 首先建立一个类库项目TransferPicLib,导入wcf需要的引用System.Service ...

  5. pwd 命令

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...

  6. 出现windows启动服务失败(无法从命令行或调试器启动,需要安装InstallUtil.exe)的解决办法

    两种方法1 从命令行安装2 选择项目-视图-自定义操作   然后将 安装,提交,回滚,卸载分别加入自定义操作,注意InstallClass属性为true

  7. 从零开始学android -- Service

    废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...

  8. Spring整合JMS(消息中间件)

    这一节来说说,异步机制及spring对JMS封装 一.消息异步处理 类似于RMI.Hessian.Burlap等远程方法调用,它们都是同步的,所谓同步调用就是客户端必须等待操作完成,如果远程服务没有返 ...

  9. shader随记

    o.WorldPos = normalize(mul((float4x4)unity_ObjectToWorld, v.vertex)).xyz;

  10. PHP中foreach用法详细讲解

    1.foreach是什么? foreach是PHP的一种语法结构,其实就是一个工具,(工具:就是工作的时候用到的器具),那么在程序开发过程中,为了达到程序效果,就用到了foreach. 2.如何用? ...