LintCode_097_二叉树的最大深度
来源:互联网 发布:一键抠图软件怎么清理 编辑:程序博客网 时间:2024/06/07 16:32
问题描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的距离。
样例
给出一棵如下的二叉树:
这个二叉树的最大深度为3.
求解
注意题目中“深度”的定义与标准定义不同,标准定义中“深度”指根结到某结点路径的长度,样例中最大深度应为2.
以下是按题目要求给出的代码:
class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here if(root == nullptr) return 0; int leftDepth = maxDepth(root -> left) + 1; int rightDepth = maxDepth(root -> right) + 1; return std::max(leftDepth, rightDepth); }};
阅读全文
0 0
- LintCode_097_二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 【网络流24题】最长递增子序列(DP+最大流)
- linux高级编程基础系列:线程间通信
- Iput设备使用分析--1(驱动层面)
- python检测变量名
- Zhang-shasha算法
- LintCode_097_二叉树的最大深度
- Peer's certificate issuer has been marked as not trusted by the user
- 服务器top命令cpu利用率1000%状况
- win7上装Linux的一些注意事项
- <s:select >标签的使用
- FTP连接模式分析
- ZooKeeper介绍
- java集合类的讲解
- 微信小程序 chooseImage选择图片或者拍照