Skip to content
On this page

111. Minimum Depth of Binary Tree

https://leetcode.com/problems/minimum-depth-of-binary-tree/

js
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
function minDepth(root) {
  if (!root) {
    return 0
  }

  if (root.left && root.right) {
    return 1 + Math.min(minDepth(root.left), minDepth(root.right))
  }

  if (root.left) {
    return 1 + minDepth(root.left)
  }

  if (root.right) {
    return 1 + minDepth(root.right)
  }

  return 1
}
js
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
function minDepth(root) {
  if (!root) {
    return 0
  }

  let depth = 1
  let current = [root]

  while (current.length) {
    const next = []

    for (let node of current) {
      if (!node.left && !node.right) {
        return depth
      }
      if (node.left) {
        next.push(node.left)
      }
      if (node.right) {
        next.push(node.right)
      }
    }

    depth += 1
    current = next
  }

  return depth
}
py
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution(object):

    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        if root.left and root.right:
            return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
        if root.left:
            return 1 + self.minDepth(root.left)
        if root.right:
            return 1 + self.minDepth(root.right)
        return 1
py
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution(object):

    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0

        depth = 1
        current = [root]

        while current:
            nxt = []
            for node in current:
                if not node.left and not node.right:
                    return depth
                if node.left:
                    nxt.append(node.left)
                if node.right:
                    nxt.append(node.right)
            depth += 1
            current = nxt

        return depth
go
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}

	ld := minDepth(root.Left)
	rd := minDepth(root.Right)

	if ld == 0 {
		return 1 + rd
	}
	if rd == 0 {
		return 1 + ld
	}
	if ld > rd {
		return 1 + rd
	}
	return 1 + ld
}
go
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}

	depth := 1
	current := []*TreeNode{root}

	for len(current) > 0 {
		var next []*TreeNode

		for _, node := range current {
			if node.Left == nil && node.Right == nil {
				return depth
			}
			if node.Left != nil {
				next = append(next, node.Left)
			}
			if node.Right != nil {
				next = append(next, node.Right)
			}
		}

		depth += 1
		current = next
	}

	return depth
}