二叉树的最大深度
来源:互联网 发布:新歌2016网络红歌 编辑:程序博客网 时间:2024/06/15 15:20
1.问题描述:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的距离。
2.思路:正如样例
给出一棵如下的二叉树:
1 / \ 2 3 / \ 4 5
这个二叉树的最大深度为3
利用递归的思想,不断求左右子树的深度,最终最大的那个+1就是给定二叉树的最大深度。
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 l=maxDepth(root->left); int r=maxDepth(root->right); int a=max(l,r)+1; return a; }};
0 0
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- CCameraDS
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
- 为列表框控件List Control设置行高
- 使用反射的步骤
- RecycleView添加下滑到底部的监听
- 二叉树的最大深度
- 关于Webkit的JS数据抓取(记录)
- 正则表达式 基础匹配中文、数字和字母
- TFTP协议的实现
- Failed to contact Jack server;No Jack Server running ;Android7.0(N)Jack 不支持多人同时在线编译。
- ubuntu 16.04安装jdk出现“java已包含以下软件包”问题的解决方法
- Linux 配置永久DNS解析服务器的方法
- 二叉树的中序遍历
- windows driver kit 7.1.0(简称为wdk7.1) 7600_1 安装方法