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. mysql笔记

    查看当前版本: SELECT VERSION(); 查看当前时间: SELECT NOW(); 查看当前当前用户: SELECT USER(); 创建数据库:CREATE DATABASE 数据库名字 ...

  2. java中使用mongodb的几种方式

    最近有时间看了一下mongodb,因为mongodb更容易扩展所以考虑使用mongodb来保存数据. 首先下载安装mongodb,这是很简单的,装好后使用mongod命令就可以启动数据库.正式部署的话 ...

  3. x01.BSheepTree: 树

    数据结构,无外乎三: 1. 一对一,线性表,数组是也: 2. 一对多,树,菜单是也: 3. 多对多,图,网络是也. 涉及到树,有一个平衡的问题,左旋转,右旋转,转得人晕晕乎乎.好在陈广的<数据结 ...

  4. ajax 允许跨域html头设置

    "Access-Control-Allow-Origin"= "*" "Access-Control-Allow-Headers"= &qu ...

  5. 禁用Windows重复数据删除

    重复数据删除,可以减少磁盘占用,但使用不当也有可能增加IO,另外,也为此功能会将硬盘分块,所以当硬盘占用较高时,进行碎片整理也比较困难,所以有时需要禁用掉重复数据删除功能,并解除重复数据的优化,可以通 ...

  6. IIS 启用CORS ,IISExpress 通过IP 访问

      在IIS 10中启用CORS: <system.webServer> <handlers> <remove name="OPTIONSVerbHandler ...

  7. NFS指定端口

    nfs服务端: 编辑/etc/nfsmount.conf,在末尾添加: RQUOTAD_PORT=30001LOCKD_TCPPORT=30002LOCKD_UDPPORT=30002MOUNTD_P ...

  8. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. UISearchController 的用法[点击搜索框,自动到顶部]

    //在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...

  10. form 表单基础知识

    <form method=" name="one" action="http://www.battlenet.com.cn/zh/"> & ...