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 ...
随机推荐
- Windows Server 2012 虚拟化实战:网络(二)
关于Windows Server的虚拟化网络,前文描述了在操作系统层面上的出现的配置变化.其中的一些配置通过Windows Server提供的小工具即可实现,如网卡组的配置,而有些需要安装Window ...
- ios10.2真机调试包,ios升级10.2后需要添加
下载地址: http://download.csdn.net/detail/koktear/9710820 添加地址: finder-应用程序-找到Xcode-右击显示包内容-Contents-Dev ...
- 例解 Linux cd 命令
cd 命令是 *nix 系统中最基本的命令,它所做的事情是改变你当前所在的目录.本文详细介绍该命令,它所能完成的功能以及关于该命令内在的东西. cd 命令:一个内置命令 BASH Shell 是大多 ...
- Salesforce Apex 使用JSON数据的示例程序
本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...
- swift 简介和常量与变量 --- swift 入门
一.思维导图 二. 代码 //创建UIView 和按钮 let views = UIView(frame: CGRect(x:20, y: 20, width: 100, height: 100))/ ...
- 制作Mac安装盘U盘
1. 下载对应版本的mac安装文件, 复制到mac上, 解压后应该是一个类似于 Install OS X [version name].app 的目录, 复制到/Applications 2. 将U盘 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [翻译] V8引擎的解析
原文:Parsing in V8 explained 本文档介绍了 V8 引擎是如何解析 JavaScript 源代码的,以及我们将改进它的计划. 动机 我们有个解析器和一个更快的预解析器(~2x), ...
- 【BZOJ 3876】【AHOI 2014】支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 这道题每条支线的意思是每条边... 那么每条边的下界设为1就行了. 这样建出一个DAG,每条边 ...
- 搭建web框架手册(一)
昨天听完永康对EASYUI的介绍后终于明白了优秀的UI框架就是第一生产力,过去自己一直沉浸在后端代码中,完全忽视了前端的生产力交互,总觉得界面漂亮就是生产力,其实大错特错,真正的具有高效生产力的界面其 ...