二叉树的最大深度-LintCode
来源:互联网 发布:java cookies和session 编辑:程序博客网 时间:2024/05/16 08:48
描述:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的距离。
样例:
给出一棵如下的二叉树:
1 / \ 2 3 / \ 4 5
这个二叉树的最大深度为3
.
思路:
这个题就是递归,不断返回左右子树中较大的高度。
代码:
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here if(root==NULL) return 0; int lheight=0, rheight=0; lheight=maxDepth(root->left); rheight=maxDepth(root->right); if(lheight>rheight) return lheight+1; else return rheight+1; }};
0 0
- LintCode:二叉树的最大深度
- 二叉树的最大深度(LintCode)
- lintcode,二叉树的最大深度
- LintCode-二叉树的最大深度
- lintcode 二叉树的最大深度
- LintCode 二叉树的最大深度
- 二叉树的最大深度-LintCode
- LintCode 二叉树的最大深度
- lintcode 97 二叉树的最大深度
- [lintcode]二叉树的最大深度
- 二叉树的最大深度-LintCode
- [LintCode]97.二叉树的最大深度
- lintcode二叉树的最大深度
- lintcode 97.二叉树的最大深度
- [Lintcode] #97 二叉树的最大深度
- LintCode-剑指Offer-(97)二叉树的最大深度
- lintcode——二叉树的最大深度
- LintCode [递归]97.二叉树的最大深度
- C/C++插入一个元素的思路
- Spring Boot干货系列:(三)启动原理解析
- memset() 初始化类对象
- MySQL通过游标来实现通过查询结果集循环
- IDEA破解 2017 IDEA license server 激活(可用)(转载)
- 二叉树的最大深度-LintCode
- 将redis设置为系统服务
- 【2016东莞信息特长生】子数整数
- 主机中配置yum仓库,并安装gcc编辑器
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- BoxCollider修改Size大小问题。Unity3d的坑
- 金额四舍五入保留n位小数
- Spark机器学习之聚类
- 判断时间间隔两分钟