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 ...
随机推荐
-
javascript-桥接模式
桥接模式 1.在系统沿着多个维度变化的同时,又不增加其复杂度并以达到解耦 2.最主要特点:将实现层(如元素绑定的事件)与抽象层(如修饰页面UI逻辑)解耦分离,使两部分独立变化 3.避免需求的改变造成对 ...
-
npm install 出现UNABLE_TO_GET_ISSUER_CERT_LOCALLY
解决方式 As a workaround you can turn ssl checking off in your .npmrc 执行 npm config set strict-ssl false ...
-
WIN7下强制分第四个主分区的方法
通过磁盘管理的界面方式, 第四个分区会被分成扩展分区, 建议通过命令行 打开命令行运行diskpart, list disk 会列出所有磁盘, 选择要操作的磁盘序号如1,select disk 1 如 ...
-
【Mysql学习笔记】浅析mysql的binlog
最近读一份关于“数据库事务故障恢复"的技术资料,发现对mysql的binlog的认识不够清楚,查阅mysql reference manual有所收获,作为笔记,记录于此. 1. What' ...
-
HTML之<;!DOCTYPE>; 标签介绍
实例: <!DOCTYPE html> <html> <head> <title>文档的标题</title> </head> & ...
-
SQL 左外连接查询 将右表中的多行变为左表的一列或多列
示例: --行列互转 /**************************************************************************************** ...
-
linux利用CMakeLists编译cuda程序
文件目录: cudaTest |--utils.cu |--utils.h |--squaresum.cu |--squaresum.h |--test.cpp |--CMakeLists.txt 编 ...
-
水果(map的嵌套)
夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了 ...
-
2019swpuj2ee作业一:C/S,B/S的应用的区别
1.硬件环境不同: C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网之间再通过专门服务器提供连接和数据交换服务.B/S 建立在广域网之上的, 不必是专门的网络硬件环境,例与电话上网, ...
-
Hive-1.2.1_03_DDL操作
Hive官方文档:Home-UserDocumentation Hive DDL官方文档:LanguageManual DDL 参考文章:Hive 用户指南 注意:各个语句的版本时间,有的是在 hiv ...