leetcode 111 minimum depth of binary tree
problem description:
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.
problem analysis:
第一步,设计演算法:遍历一棵树的方法有两种,BFS and DFS,思考了下,BFS是一圈一圈的扩张出去,而这个problem是计算最小的深度,所以BFS可能更适合这个problem。
1.root is the layer 1
2.search out the node in layer 2 by layer1
3.check whether there is a leaf in layer 2: if true, return current layer; if false, continue iteration until finding a leaf.
第二步,设计data structure。在BFS中,优先考虑的data structure是queue,可是在c language中,并没有现成的queue container。那么how to solve this small problem。使用了两个数组,两个index,用来表示数组的长度,这样就实现了一个长度可变的数组。一个数组用来store父节点,另外一个数组用来store孩子节点。当一次iteration结束后,将孩子节点数组的内容移动到父节点数组,孩子节点数组清除为0,在code中,将孩子节点数组的index置为0表示数组当前值无效。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int minDepth(struct TreeNode* root) {
//special case 1
;
//special case 2
;
int numOfParent, numOfChildren;
;
];
];
//initialization
parent[] = root;
numOfParent = ;
numOfChildren = ;
//start iteration from root
while(true)
{
//calculate children
;
; i<numOfParent; i++)
{
if(parent[i]->left) {children[counter]=parent[i]->left; counter++;}
if(parent[i]->right) {children[counter]=parent[i]->right; counter++;}
}
//store the length of children in numOfChildren, children's level in layer
numOfChildren = counter;
layer++;
//check whether there is a leaf in children
; k<numOfChildren; k++)
{
if( (children[k]->left==NULL) && (children[k]->right==NULL) ) return layer;
}
//preparation for next iteration
numOfParent = numOfChildren;
; m<numOfChildren; m++)
{
parent[m] = children[m];
}
numOfChildren = ;
}
}
leetcode 111 minimum depth of binary tree的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [LeetCode] 111. 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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode 111. 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 111 Minimum Depth of Binary Tree ----- java
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java for LeetCode 111 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 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- 关于Http状态码
Http状态码表示Http协议所返回的响应的状态.比如客户端向服务器发送请求,如果成功的获得请求的资源,则返回的状态码为200,表示相应成功.如果请求的资源不存在,则通常返回404错误. Http状态 ...
- 一道关于java序列化的问题,看大家知多少————
问题先放在这里,稍后我会做出解答 已知类有Test和Test2,问两次主程序的输出结果是多少(SerializeUtil只是序列化的工具类) 类Test public class Test imple ...
- 开窗函数 First_Value 和 Last_Value
在Sql server 2012里面,开窗函数丰富了许多,其中带出了2个新的函数 First_Value 和 Last Value .现在来介绍一下这2个函数的应用场景. 首先分析一下First_Va ...
- Arduino 1602液晶屏实验和程序
在Arduino IDE中, 项目->加载库->管理库中搜索LiquidCrystal,然后安装即可 1.接线图 2.引脚图 3.最简单程序 #include <LiquidCrys ...
- vi(vim)键盘图及其基本命令
进入vi vi filename 打开或新建文件,并将光标置于第一行首 vi +n filename 打开文件,并将光标置于第 n行首 vi + fi ...
- plain framework 1 1.0.3更新 优化编译部分、网络压缩和加密
有些东西总是姗姗来迟,就好比这新年的钟声,我们盼望着新年同时也不太旧的一年过去.每当这个时候,我们都会总结一下在过去的一年中我们收获了什么,再计划新的一年我们要实现什么.PF并不是一个十分优秀的框架, ...
- Lite Your Android English
https://litesuits.com/ 一些话 简约的背后,往往是复杂 还原面向对象应有的体验,让应对繁多业务所增加的,并未增加. 展开设计理念 Lite每个项目仅几十KB,这相当于你项目中 ...
- NHibernate之映射文件配置说明
NHibernate之映射文件配置说明 1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表 ...
- GO语言总结(2)——基本类型
上篇博文总结了Go语言的基础知识——GO语言总结(1)——基本知识 ,本篇博文介绍Go语言的基本类型. 一.整型 go语言有13种整形,其中有2种只是名字不同,实质是一样的,所以,实质上go语言有1 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...