LeetCode111 Minimum Depth of Binary Tree

来源:互联网 发布:共享景观设计 知乎 编辑:程序博客网 时间:2024/05/22 10:40

详细见:leetcode.com/problems/minimum-depth-of-binary-tree


Java Solution: github

package leetcode;import java.util.LinkedList;import java.util.Queue;/* * 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. */import tools.TreeNode辅助.TreeNode;public class P111_MinimumDepthofBinaryTree {public static void main(String[] args) {}/* * AC * 1 ms */static class Solution {    public int minDepth(TreeNode root) {    if (root == null) {    return 0;    }    Queue<TreeNode> q1 = new LinkedList<>();    Queue<Integer> q2 = new LinkedList<>();    q1.add(root);    q2.add(1);    int depth_now = 1;    while (! q1.isEmpty()) {    TreeNode root_now = q1.poll();    depth_now = q2.poll();    if (root_now.left == null && root_now.right == null) {    return depth_now;    }    if (root_now.left != null) {    q1.add(root_now.left);    q2.add(depth_now + 1);    }    if (root_now.right != null) {    q1.add(root_now.right);    q2.add(depth_now + 1);    }    }        return depth_now;    }}}


C Solution: github

/*    url: leetcode.com/problems/minimum-depth-of-binary-tree    AC 6ms 28.17%*/#include <stdio.h>#include <stdlib.h>struct TreeNode {    int val;    struct TreeNode *left;    struct TreeNode *right;};typedef struct TreeNode stn;typedef struct TreeNode * ptn;ptn tn_init(int val) {    ptn n = (ptn) malloc(sizeof(stn));    n->val = val;    n->left = NULL;    n->right = NULL;    return n;}typedef ptn T;typedef struct al sal;typedef struct al * pal;struct al {    int capacity;    int size;    T* arr;};pal al_init(int capacity) {    pal l = (pal) malloc(sizeof(sal));    if (capacity < 1) return NULL;    l->arr = (T*) malloc(sizeof(T) * capacity);    l->capacity = capacity;    l->size = 0;    return l;}void al_expand_capacity(pal l) {    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));    int i = 0;    for (i = 0; i < l->capacity; i ++)        new_arr[i] = l->arr[i];    free(l->arr);    l->arr = new_arr;    l->capacity = l->capacity * 2 + 1;}void al_add_last(pal l, T v) {    if (l->capacity == l->size) al_expand_capacity(l);    l->arr[l->size] = v;    l->size ++;}void al_free_all(pal l) {    free(l->arr);    free(l);}void swap_al(pal* l1, pal* l2) {    pal t = *l1;    *l1 = *l2;    *l2 = t;}int minDepth(ptn n) {    pal l1 = NULL, l2 = NULL;    int cnt = 0, i = 0, sign = 0;    ptn tn = NULL;    if (n == NULL) return 0;    l1 = al_init(16);    l2 = al_init(16);    al_add_last(l1, n);    while (l1->size != 0) {        l2->size = 0;        cnt ++;        sign = 1;        for (i = l1->size-1; sign && i >-1; i --) {            tn = l1->arr[i];            sign = !(tn->left == NULL && tn->right == NULL);            if (tn->left != NULL) al_add_last(l2, tn->left);            if (tn->right != NULL) al_add_last(l2, tn->right);        }        if (! sign) break;        swap_al(&l1, &l2);    }    al_free_all(l1);    al_free_all(l2);    return cnt;}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/minimum-depth-of-binary-tree    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月29日    @details:    Solution: 66ms 83.33%'''class TreeNode(object):    def __init__(self, x):        self.val = x        self.left = None        self.right = Noneclass Solution(object):    def minDepth(self, n):        """        :type n: TreeNode        :rtype: int        """        if n == None: return 0        l1, a = [n], 0        while len(l1) != 0:            a, l2 = a+1, []            for l in l1:                if l.left == None and l.right == None: return a                if l.left != None: l2.append(l.left)                if l.right != None: l2.append(l.right)            l1 = l2        return a        


0 0