一.题目

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. 【Android 应用开发】Android 平台 HTTP网速測试 案例 API 分析

    作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速測试标准 : 除普通网页測速 ...

  2. SAN (Storage Attached Network),即存储区域网络

    NAS和SAN既竞争又合作,很多高端NAS的后端存储就是SAN.NAS和SAN的整合也是存储设备的发展趋势,比如EMC的新产品VNX系列. 关于NAS和SAN的区别,可以列出很多来.比如带宽大小,距离 ...

  3. [POJ 1316] 树上的询问

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...

  4. GCD的小结

    同步和异步的区别 同步:在当前线程中执行 异步:在另一条线程中执行 有4个术语比较容易混淆:同步.异步.并发.串行 同步和异步决定了要不要开启新的线程 同步:在当前线程中执行任务,不具备开启新线程的能 ...

  5. 关于C语言中EOF的一点认识

    总结来说:EOF(即End Of File)是一个文件结束的标记,当文件被读取到EOF位置时,参与读取的函数会返回整型值 -1,这时要注意的是:这个值被赋值给有符号char类型时是0xff,被赋值给有 ...

  6. (转载)Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

    Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow   这是一张QQ空间说说详情的截图. 分析: 1.点击右上角三个点的图标,在界面底部弹出一个区域,这个 ...

  7. Python学习——爬虫篇

    requests 使用requests进行爬取                 下面是我编写的第一个爬虫的脚本                   import requests # 导入reques ...

  8. mac pro 安装 composer 失败

    http://getcomposer.org/doc/00-intro.md#using-composer $ brew install josegonzalez/php/composer 出现错误: ...

  9. Unity 手机屏幕翻转问题 横屏

    1920*1080的图在1080*1920的设备上观看效果: 如果要做横屏游戏,就要改Build中的Player Settings,强制左旋转或右旋转,默认是Auto 垂直于地面的手机在横屏下分辨率是 ...

  10. poj1050查找最大子矩阵和

    题目: To the Max   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 48507   Accepted: 2566 ...