一.题目

Maximum Depth of Binary Tree

Total Accepted: 70693 Total Submissions: 156776My
Submissions

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.

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss








二.解题技巧

    这道题和Minimum Depth of Binary Tree类似,仅仅只是这个是求最大深度的。解法也和Minimum
Depth of Binary Tree
一样,都是对二叉树进行递归。然后将子树的最大深度进行返回。终于得到这个树的最大深度。
    这种方法的时间复杂度为O(n),空间复杂都为O(logn)。


三.实现代码

#include <iostream>

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ 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)
{
return 0;
} int Result = 1;
int Left = maxDepth(root->left);
int Right = maxDepth(root->right); if (Left * Right)
{
Result += Left < Right? Right : Left;
}
else
{
Result += Right + Left;
} return Result;
}
};



四.体会

    这是一道类似的题,仅仅是改改代码就能够实现了。



版权全部,欢迎转载,转载请注明出处。谢谢




LeetCode_Maximum Depth of Binary Tree的更多相关文章

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

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

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

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

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

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

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  9. 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. Suffix Array 后缀数组

    后缀数组 顾名思义.SuffixArray(下面有时简称SA) 和字符串的后缀有关. 后缀:字符串中某个位置一直到结尾的子串.(SA中讨论包含了原串和空串).所以共同拥有len+1个后缀. 后缀数组: ...

  2. HTTP Error 500.19

    HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related ...

  3. 【HDU 2176】 取(m堆)石子游戏

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2176 [算法] Nim博弈 当石子数异或和不为0时,先手必胜,否则先手必败 设石子异或和为S 如果 ...

  4. Redis学习笔记(六) 基本命令:List操作

    原文链接:http://doc.redisfans.com/list/index.html lpush key value[value...] 将一个或多个value插入到列表的表头:例:lpush ...

  5. Asp.Net Core部署到Linux服务器

    从2016年7月, .NET Core1.0 正式发布开始,由于时间问题,我没怎么关注过.NET Core,最近刚抽出点时间研究了下,先讲下如何把ASP.NET Core部署到Linux上吧.这里我用 ...

  6. 欢迎来到Flask的世界

    不多说,直接上文档链接:Flask的文档 教程 API 快速上手

  7. Centos7 minimal 系列之rabbitmq安装(八)

    一.安装Erlang 由于RabbitMQ依赖Erlang, 所以需要先安装Erlang. 这种方法网站访问不了 wget https://packages.erlang-solutions.com/ ...

  8. js-apply call bind 浅析

    call 1.第一个参数指定了this,第二个参数传给this,也就是call前面的函数,作为他的参数第三个参数也一样 指定了this,就是执行环境,greet的this在i里面找 function ...

  9. tp5数据库操作 Db类

    一.链接数据库 1.配置文件定义  application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...

  10. SQL Server-简单查询语句,疑惑篇

      前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开 ...