题目:

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.

代码:

/**
* Definition for a binary tree node.
* 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 ;
if ( !root->left && !root->right ) return ;
return std::max(Solution::maxDepth(root->left)+, Solution::maxDepth(root->right)+);
}
};

tips:

比求最小叶子节点深度容易一些。

========================================

第二次过这道题,一次AC。

/**
* Definition for a binary tree node.
* 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 ;
return max(Solution::maxDepth(root->left), Solution::maxDepth(root->right))+;
}
};

【Maximum Depth of Binary Tree 】cpp的更多相关文章

  1. 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

  2. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  3. 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

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

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

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

  6. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

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

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

  9. 2016.6.26——Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...

随机推荐

  1. linux下proc目录详解

    proc/pid记录了什么cd /proc/之后,你会发现很多的目录和文件,今天首先来介绍的就是那些以数字命名的目录--它们就是linux中的进程号,每当你创建一个进程时,里面就会动态更新多出一个名称 ...

  2. winfrom 导入Excel表到access数据库(来自小抽奖系统)

    网上有很多这种方法,本人只是针对自己的系统来实现的 //导入excel表 private void ImportTSMenu_Click(object sender, EventArgs e) { O ...

  3. css中li、a、span行内强制不换行

    li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后

  4. 5.21_启程日本二面_1 vs 1

    昨天上午刚群面完,晚上7点左右就接到了电话.面试官就两位菇凉,看来她们也是很辛苦.今天下午3点 1 vs 1,在一家咖啡店里,主要是询问下去日本的意愿是否足够强烈.太老实,这里实话实说,也没有表现出非 ...

  5. spark-submit工具参数说明

    执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...

  6. js浏览器各种位置检测

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 生成不重复随机数,int转 TCHAR 打印输出

    在0~n 中 随机去除不重复的k个数 int k=100; int n=80000; for(int i=0;k>0&&i<n;i++) { if((bigrand()%( ...

  8. xcode编译错误总结

    No architectures to compile for(ONLY_ACTIVE_ARCH=YES 这种错误    修改building settings下 Build Active Archi ...

  9. iOS开发常用的宏

    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([UIScreen mainS ...

  10. 刀哥多线程之一次性代码gcd-11-once

    一次性执行 有的时候,在程序开发中,有些代码只想从程序启动就只执行一次,典型的应用场景就是"单例" // MARK: 一次性执行 - (void)once { static dis ...