Python solution using queue asking for improvement


  • 0
    A

    class Solution(object):
    def minDepth(self, root):
    """
    :type root: TreeNode
    :rtype: int
    """
    depth =0;
    q = [];
    q.append(root);
    while q:
    flag = False; #Flag only increase once in one level
    qlen = len(q);
    for i in range(0,qlen):
    cur = q.pop(0);
    if cur:
    if not cur.left and not cur.right:
    if flag:
    return depth;
    else:
    return depth+1;
    else:
    q.append(cur.left)
    q.append(cur.right)
    if cur and not flag:
    flag = True;
    depth =depth+1;
    return depth


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.