LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离)
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.
有两种求解的思路,一种采用DFS的思想,一种采用BFS的思想,如下代码所示:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
};
//采用DFS的思想
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return l > r ? l + :r+;
//以上这两种方式有一种更简便的方法
//return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
//采用BFS的方法,引入队列
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
queue <TreeNode *> que;
int nCount = ;
int nDepth = ;// 记录队列里面每一层上的元素
que.push(root);
while(!que.empty()) {
TreeNode *pTemp = que.front();
que.pop();
nCount --;
if (pTemp->left)
que.push(pTemp->left);
if (pTemp->right)
que.push(pTemp->right);
if (nCount == ) {
nDepth ++;
nCount = que.size();
}
}
return nDepth;
}
LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [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 ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [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 l ...
随机推荐
- 深度学习中,使用regularization正则化(weight_decay)的好处,loss=nan
刚开始训练一个模型,自己就直接用了,而且感觉训练的数据量也挺大的,因此就没有使用正则化, 可能用的少的原因,我也就不用了,后面,训练到一定程度,accuracy不上升,loss不下降,老是出现loss ...
- java处理url中的特殊字符%等
java处理url中的特殊字符(如&,%...) URL(Uniform Resoure Locator,统一资源定位器)是Internet中对资源进行统一定位和管理的标志.一个完整的URL包 ...
- Python 学习图谱
https://mp.weixin.qq.com/s/-fJYAlOs4ui0YeJ-W6BkeQ
- linux下redis4.0.2集群部署(利用Ruby脚本命令)
一.原生命令方式和Ruby脚本方式区别 利用Ruby脚本部署和用原生命令部署,节点准备的步骤都是一样的,节点启动后的握手,以及主从.槽分配,利用Ruby脚本一步就能完成,利用原生命令需要一步一步地执行 ...
- 微信H5支付 遇到坑的一些解决方法
解决办法 1. 商家参数格式有误,请联系商家解决 a.对于前后端分离的开发模式 前端发起请求 服务端请求微信h5支付统一下单接口 返回参数mweb_url 给前端 然后前端调起微信h5支付 b.注意的 ...
- 从汇编层面解释switch语句判断快速的原因
源码如下: #include <stdio.h> void main(){ int flag; flag=1; switch (flag){ ...
- 【mac环境】终端配色 & 配置使用ll命令
1.MAC OS X 命令终端的颜色显示 打开 terminal 会发现 ls 和 grep 后的结果是没有色彩的,这时候可以这么干: 用 vim 打开文件 ~/.bash_profile,然后把下边 ...
- redis 3.2.1 centos7
设置外网访问 vmare:centos7redis:3.2.1 redis-cli shutdown 重启./redis-server 启动服务 启动之后无法访问 cd redis-3.2.1 vim ...
- latex相关概念
关于Latex,收到网友的鼓励,决定好好整理下相关的信息. 在初次使用相关的程序时,遇到很多迷惑的概念,下面这篇帖子汇总得很详细. 关于latex各种概念与理解 帖子中提到了三个概念,引擎,宏集(即下 ...
- linux下面重启apche 与mysql服务
1.service httpd restart 重启apache 2.service mysqld restart 重启mysql 开启与停止换成start与stop即可