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的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  2. [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 ...

  3. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. (二叉树 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  2. 关于Http状态码

    Http状态码表示Http协议所返回的响应的状态.比如客户端向服务器发送请求,如果成功的获得请求的资源,则返回的状态码为200,表示相应成功.如果请求的资源不存在,则通常返回404错误. Http状态 ...

  3. jquery基础

    show() hide() toggle()         fadeIn() fadeOut() fadeToggle() fadeTo()         slideUp() slideDown( ...

  4. MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL

    MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL   是因为目标板的芯片处于休眠 ...

  5. Android APP 简单高效的禁用横竖屏切换

    默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏 ...

  6. chrome中不可见字符引发的float问题

    起因是刷知乎时碰到这么个问题:https://www.zhihu.com/question/41400503 问题代码如下: <!DOCTYPE html> <html lang=& ...

  7. 基于Vue.js的表格分页组件

    有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...

  8. N-Queens

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  9. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  10. PyVISA介绍

    针对测量仪器进行编程比较痛苦,存在各种各样的协议以及通过不同接口和总线(GPIB.USB.RS232).使用任何一种语言去编程,你必须找到支持仪器和对应总线的合适的库. 为了解决这种问题,VISA应运 ...