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. Jmeter3.0-多维度的图形化HTML报告

    本文转载于推酷:http://www.tuicool.com/articles/BNvuEzr 在JMeter3.0之前,官方只提供在工具的UI上对测试结果部分维度的图形化展示,这对我带来了两方面的困 ...

  2. Repeater绑定List泛型对象

    后台: public void BindData()        {            List<WeiBo> DataList = new List<WeiBo>(); ...

  3. laravel模型建立和数据迁移和数据填充(数据填充没有成功)未完

    开始创建我们的第一个 Article 模型及其对应迁移文件了,我们在项目根目录运行如下 Artisan 命令一步到位: php artisan make:model Article -m -m 是 - ...

  4. 非常easy学习的JQuery库 : (二) 选择器

    作用 选择器同意您对元素组或单个元素进行操作. 在前面的章节中,我们介绍了一些有关怎样选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是怎样准确地选取您希望应用效果的元素. jQuer ...

  5. python socket编程(socket)

    代码如下: server端: import sockets=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host=socket.gethostnam ...

  6. Python之内置类型

    python有6大内置类型 数字.序列.映射.类.实例.异常. 下面就慢慢来说明: 1.数字 有3个明确的数字类型,整型,浮点型及复数.另外,布尔是整型的一个子类型. (另外标准库还包含额外的数字类型 ...

  7. mapreduce中reduce中的迭代器只能调用一次。其实迭代器就只能调用一次

    亲测,只能调用一次,如果想想在一次reduce重复使用迭代器中的数据,得先取出来放在list中然后在从list中取出来!!多次读取reduce函数中迭代器的数据 public static void ...

  8. sublime使用技巧(1)-- 下载与插件安装

    一.下载 到官网下载最新的版本 https://www.sublimetext.com/ Sublime官方插件网站 https://packagecontrol.io/ 二.插件安装配置 1.打开S ...

  9. Oracle关于快速缓存区应用原理

    为什么oracle可以对于大量数据进行訪问时候能彰显出更加出色表现,就是通过所谓的快速缓存来实现数据的快速运算与操作.在之前的博文中我已经说过sql的运行原理,当我们訪问数据库的数据时候,首先不是从数 ...

  10. ftp put get 的使用方法

    首先:ftp user@ip 登录到远程主机 成功后,输入ls,如下: ftp> ls 可以得到远程主机的本地目录. 一:get命令(从远程主机下载文件到本机): ftp> get (re ...