Minimum Depth of Binary Tree--LeetCode
来源:互联网 发布:cae仿真软件 编辑:程序博客网 时间:2024/06/04 19:21
题目:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思路:这里递归就需要注意一些东西了,对于非递归来说,只要发现有一个节点没有左子树同时也没有右子树,那么这个就是节点所在层次就是目标
int MinDepth(BinTree* root){if(root == NULL)return 0;if(root->left == NULL)return MinDepth(root->right)+1;if(root->right == NULL)return MinDepth(root->left)+1;return min(MinDepth(root->left),MinDepth(root->right))+1;}int MinDepth_second(BinTree* root){if(root == NULL)return 0;BinTree* node;deque<BinTree*> de;int num;int curnum=0;int level=0;de.push_back(root);num=1;while(!de.empty()) {node = de.front();de.pop_front();num--;if(node->left==NULL && node->right == NULL)return level+1;if(node->left != NULL){de.push_back(node->left);curnum++;}if(node->right !=NULL){de.push_back(node->right);curnum++;}if(num == 0){num = curnum;curnum=0;level++;}}return level;}
0 0
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- [Leetcode]Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- Leetcode: Minimum Depth of Binary Tree
- LeetCode-Minimum Depth of Binary Tree
- [leetcode] Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 【Leetcode】Minimum Depth of Binary Tree
- LeetCode | Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 字符集总览
- mfc下添加皮肤文件 SkinPPWTL,.ssk
- IP数据报首部checksum的计算
- Java学习之路-序列化与反序列化
- 配置VS2008自带的SQL Server 2005 Express sa登陆
- Minimum Depth of Binary Tree--LeetCode
- C++ 倒排索引的实现
- tomcat7+apache2.2.25 集群负载均衡以及session复制配置
- c++实现对称矩阵的压缩存储
- ORACLE-基础(十一)Data Integrity
- eclipse:打开 eclipse 出现 “android sdk content loader 0%” 卡住的错误分析及解决方法
- 安装ubuntu后root密码提示错误以及修改root密码
- C语言数组名作为函数参数
- 算法导论:堆排序递归实现 (c语言)