题目:

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. VS2013添加NuGet的方法

    1.工具->扩展和更新->联机 2.右上角搜索框搜索NuGet,选择NuGet Package Manager for Visual Studio 2013,安装后重启VS 下面通过添加N ...

  2. 首页banner特效

     <link href="css/swiper.min.css" rel="stylesheet" />  <script src=" ...

  3. dedecms 调用channel子栏目的id问题

    dedecms 说明文档:http://www.dedecms.com/archives/templethelp/help/taghelp.htm {dede:channel type='son' t ...

  4. WinRt BottomAppBar

    BottomAppBarDemo.xaml <Page.BottomAppBar> <AppBar> <StackPanel Orientation="Hori ...

  5. POJ C程序设计进阶 编程题#4:Tomorrow never knows?

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 甲壳虫的<A day ...

  6. c#获取多个List<class>合并、并将相同条件下的值累计sum

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Linux redis 配置文件

    # Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...

  8. 学习c的第7天

    #include <stdio.h> int main() { int x=0; if (x==0) { printf("x为假\n"); } else { print ...

  9. js闭包理解实例小结

    Js闭包 闭包前要了解的知识  1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...

  10. C# 平时碰见的问题【4】

    1. 模糊查询 like的参数化写法 string keyword="value"; // 要模糊匹配的值 错误示范:   sql:    string strSql=" ...