一.题目

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. uva 10061(数学)

    题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...

  2. 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

    [033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Suppose a sort ...

  3. ym——物联网入口之中的一个Android蓝牙4.0

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...

  4. TeamTalk Android代码分析(业务流程篇)

    TeamTalk Android代码分析(业务流程篇) 1.1 总体结构 1.总体结构有点类似MVC的感觉,模块结构从上向下大体是: UI层:Activity和Fragment构成,期间包括常用的一些 ...

  5. itunes connect上传截图提示无法加载文件问题

    解决的方法: 图片名字中不能包括汉字,要英文字母或数字.

  6. 初识cocos2dx——入门第一篇

    享受"啪"的按下Enter的快感. (by云风) cocos2dx是不是个toy引擎,不同的人有不同的看法.可是你不得不承认它非常受欢迎.好多非常火的手游使用的就是cocos2dx ...

  7. Java中Array、List、Set、Map

    一.Java中数组 数组用来存放固定数量的同类元素,声明方法: T[] ref,T ref[],如int[] intAry; int intAry[].推荐用T[]的方式,后一种方式为兼容C++习惯写 ...

  8. BZOJ 3052 树上带修莫队

    思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...

  9. caffe.bin caffe的框架

    最近打算看一看caffe实现的源码,因为发现好多工作都是基于改动网络来实现自己的的目的.比如变更目标函数以及网络结构,以实现图片风格转化或者达到更好的效果. 深度学习框架 https://mp.wei ...

  10. Incorrect integer value: '' for column 'RegNum' at row 1

    数据库版本: 5.6.16​​操作系统: Red Hat Enterprise Linux Server release 6.3 (Santiago)​​在插入数据的时候提示告警:​mysql> ...