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.

思路:

递归求解二叉树的深度,左右子树较深的+1(。解释主要原因:存在根节点)

实现代码:

/**
* 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==0)
{
return 0;
}
int left=maxDepth(root->left);
int right=maxDepth(root->right);
if(left>right)
{
return left+1;
}
else
{
return right+1;
}
}
};

leetcode: Maximum Depth of Binary Tree的更多相关文章

  1. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  2. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

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

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

  5. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  6. LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. LeetCode 104. Maximum Depth of Binary Tree

    Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  9. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

随机推荐

  1. jsp输出九九乘法表

    <% String st = ""; for(int i = 1; i <= 9; i++){ for(int j = 1; j <= i; j++){ st + ...

  2. 机器学习(七) PCA与梯度上升法 (上)

    一.什么是PCA 主成分分析 Principal Component Analysis 一个非监督学的学习算法 主要用于数据的降维 通过降维,可以发现更便于人类理解的特征 其他应用:可视化:去噪 第一 ...

  3. 解决VMware Pro 14安装Linux镜像时黑屏问题

    软件及版本: VMware-workstation-full-14.0.0-6661328 CentOS-6.8-x86_64-bin-DVD1 系统: win10 问题: 启动虚拟机,配置完cent ...

  4. Markdown编辑器 常用语法

    一.标题 示例: # 1这是一级标题 ## 2这是二级标题 ### 3这是三级标题 #### 4这是四级标题 ##### 5这是五级标题 ###### 6这是六级标题 效果如下: 1这是一级标题 2这 ...

  5. 洛谷P2196 && caioj 1415 动态规划6:挖地雷

    没看出来动规怎么做,看到n <= 20,直接一波暴搜,过了. #include<cstdio> #include<cstring> #include<algorit ...

  6. CMSIS-RTOS的使用

    CMSIS-RTOS实现通常作为库提供.要将RTOS功能添加到现有的基于CMSIS的应用程序,需要添加RTOS库(通常是配置文件).RTOS库的可用功能在头文件cmsis_os.h中定义,该文件特定于 ...

  7. SQL2008所有数据导出导入两种方法

    方法一:生成脚本导出导入sql2008所有数据 第一步.右键要导出的数据库.任务--生成脚本 第二步,在设置脚本编写选项处,点击--高级(A),选择要编写脚本的数据的类型为:架构和数据 假设找不到 要 ...

  8. C++关于二进制位操作小结

    #include <iostream> using namespace std; //二进制位逆序. int Grial(int x) { int n = 32; int count = ...

  9. C++学习笔记31,指向引用的指针(3)

    我们来看一个简单的指向引用的指针的样例. #include <iostream> using namespace std; int main(){ int x=10; int y=20; ...

  10. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...