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的思想,如下代码所示:
1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 //采用DFS的思想 9 int maxDepth(TreeNode *root) 10 { 11 if (NULL == root) 12 return 0; 13 int l = maxDepth(root->left); 14 int r = maxDepth(root->right); 15 16 return l > r ? l + 1:r+1; 17 //以上这两种方式有一种更简便的方法 18 //return 1 + max(maxDepth(root->left), maxDepth(root->right)); 19 } 20 21 //采用BFS的方法,引入队列 22 int maxDepth(TreeNode *root) 23 { 24 if (NULL == root) 25 return 0; 26 queue <TreeNode *> que; 27 int nCount = 1; 28 int nDepth = 0;// 记录队列里面每一层上的元素 29 30 que.push(root); 31 while(!que.empty()) { 32 TreeNode *pTemp = que.front(); 33 que.pop(); 34 nCount --; 35 36 if (pTemp->left) 37 que.push(pTemp->left); 38 if (pTemp->right) 39 que.push(pTemp->right); 40 41 if (nCount == 0) { 42 nDepth ++; 43 nCount = que.size(); 44 } 45 } 46 return nDepth; 47 }
转载于:https://www.cnblogs.com/bakari/p/4126693.html
LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy相关推荐
- 104. Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode 545. Boundary of Binary Tree 二叉树边界
LeetCode 545. Boundary of Binary Tree 二叉树边界 Given a binary tree, return the values of its boundary i ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
- Leetcode: Maximum Depth of Binary Tree
题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初 ...
- 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 ...
最新文章
- 三百年前的AI骗局:骗过美国总统和普鲁士大帝的国际象棋AI
- 主数据管理(MDM)的七个最佳实践
- 深入理解Spark 2.1 Core (八):Standalone模式容错及HA的原理与源码分析
- Java基础查漏补缺:(String篇)一个面试题问倒了我,原来String并不简单
- kubernetes(k8s)
- LeetCode Contains Duplicate III
- python文件路径改了需要重新配置环境吗_Django 设置多环境配置文件载入问题
- html怎么编辑文字位置,html – 修正文本的位置背景剪辑
- html网页模板酒店预订,MUI 项目模板之 “酒店预订”
- php中dialog使用方法,window.dialogArguments 使用说明
- 2.8数据-paddlepaddle数据集uci_housing
- c语言中感叹号什么意思_啥是c语言-c语言感叹号用法-c语言中的/和%表示什么意思...
- 解决白天黑夜模式切换导致Fragment崩溃问题
- Port 80 in use by “Unable to open process“ with PID 4!
- 《Real-Time Rendering 4th Edition》全文翻译 - 第6章 纹理化(下)6.7 ~ 6.9
- AI医疗落地挑战:需求“实打实存在”,商业价值如何挖掘?
- 模拟cmos集成电路(5)
- SaaS企业如何降低客户流失率?
- 华为交换机替换H3C交换机-割接过程
- 美国优秀教师对中国家长的建议
热门文章
- js控制select数据绑定下拉列表
- ABAP实现粘贴板的操作,复制粘贴
- I Hate It(线段树基础)
- 如何破解天翼HG260光纤猫【转】
- leetcode算法题--七进制数
- 鸿蒙2.0操作体验,鸿蒙2.0操作系统正式版-华为鸿蒙2.0操作系统正式版官方预约 v1.0.0-优盘手机站...
- 小猿圈零基础怎样学好java?
- 微服务网关Kong 1.0正式发布!提供100+项功能
- 【BZOJ】4259: 残缺的字符串 FFT
- windows下添加yaf扩展,生成yaf框架