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. ZooKeeper:Java客户端网络处理

    了解ZooKeeper客户端的实现,对于使用ZooKeeper的客户端非常重要. 通过对客户端源码的阅读,了解了如下信息: 创建ZooKeeper对象时,应会创建一个ClientCnxn(代表了客户端 ...

  2. [原创]自己动手实现React-Native下拉框控件

    因项目需要,自己动手实现了一个下拉框组件,最近得空将控件独立出来开源上传到了Github和npm. Github地址(求Star 求Star 求Star 

  3. 添加WoSign根证书到JDK

    由于某些“众所周知”的原因,Azure中国版使用了国内的WoSign证书——和臭名昭著的CNNIC有的一拼.Apple是不信任WoSign证书的,这也是为什么用Mac OS中访问www.azure.c ...

  4. 任意半径局部直方图类算法在PC中快速实现的框架。

    在图像处理中,局部算法一般来说,在很大程度上会获得比全局算法更为好的效果,因为他考虑到了图像领域像素的信息,而很多局部算法可以借助于直方图获得加速.同时,一些常规的算法,比如中值滤波.最大值滤波.最小 ...

  5. Collections.shuffle

    1.Collections.shuffler 最近有个需求是生成十万级至百万级的所有随机数,最简单的思路是一个个生成,生成新的时候排重,但是这样时间复杂度是o(n^2),网上看了几个博客的解决方法都不 ...

  6. LLDB基础知识

    LLDB基础知识 LLDB控制台 Xcode中内嵌了LLDB控制台,在Xcode中代码的下方,我们可以看到LLDB控制台. LLDB控制台平时会输出一些log信息.如果我们想输入命令调试,必须让程序进 ...

  7. pcDuino-V2利用madplay播放音乐

    在pcDuino的UBUNTU系统下,打开控制台,利用apt-get来下载madplay软件. sudo apt-get install madplay 播放音乐: madplay xxx.mp3 x ...

  8. asp.net获取服务器绝对路径和相对路径

    绝对路径 AppDomain.CurrentDomain.SetupInformation.ApplicationBase 相对路径 Server.MapPath("~/")表示当 ...

  9. 阿里云VPS服务器,ROS内网穿透

    Aliyun Windows Server 2008 R2中建立vpn服务器,ros中使用pptp拨号连接 2.在Aliyun服务器中,修改hosts,将内网分配的ip映射到指定的域名,在Aliyun ...

  10. poj2217

    Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1257   Accepted: 515 Descript ...