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 ...
随机推荐
- jQuery之动画
动画相关方法: .hide()..show()..toggle() 参数:null 或 (duration, easing, callblack) .fadeIn..fadeout ..fadeTog ...
- Tengine安装(阿里baba的)-Nginx
在先前的文章中介绍过Tengine,先前只是使用了运维人员配置好的内容,未自己进行过安装配置.周末闲来无事,对于Tengine进行了尝试性的安装.记录下面方便以后再做改进. Tengine官网上有个非 ...
- fortitoken
1.token状态为error,且不能分配给用户使用 解决: 关联有User的token状态是error的原因是:用户一直并未使用.
- 694. Number of Distinct Islands 形状不同的岛屿数量
[抄题]: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...
- The best way to use Xtool X100 PAD2 for FEM programming
Look here: XTOOL X100 PAD2 is new FEM programming. Possible to use Xtool X100 PAD2 for FEM programmi ...
- windows远程桌面连接时,显示发生身份验证错误,给函数提供的身份无效
摘自:https://www.landui.com/help/show-7787 初次看到这个错误的时候懵了.访问给的地址一看,发现大概意思是不安全了,微软要更新一下 凭据安全支持提供程序协议 (Cr ...
- FT_ND_API.dll
ePass1000ND https://blog.csdn.net/li34442779/article/details/44276989 https://www.cnblogs.com/lidabo ...
- GUI学习之一——PyQt5初识
我们在第〇篇里先演示了GUI的功能,其实Python有多个库是支持GUI编程的,python官网列出了大量的说明,其中包括了原生的tkinter 还有许多第三方库 Pyqt PySide wxPyth ...
- iDoc「文档」功能强势升级!一键分享,即刻预览!(201903-1版本更新)
小摹很想知道小伙伴们在面对众多杂乱的文档时,都是如何管理起来的呢?iDoc的「文档」功能,很多小伙伴还没有用起来,甚至都没有注意到iDoc还有这样一个功能.今天小摹就给大家介绍一下iDoc的「文档」功 ...
- rapidjson 的练习
// JsonCpp.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <string> #include < ...