Minimum Depth of Binary Tree
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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its minimum depth = 2.
Solution(javascript)
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
const minDepth = (root) => {
if (!root) {
return 0
}
const aux = (node, depth) => {
if (!node || (!node.left && !node.right)) {
return depth
}
if (node.left && !node.right) {
return aux(node.left, depth + 1)
}
if (node.right && !node.left) {
return aux(node.right, depth + 1)
}
const leftDepth = aux(node.left, depth + 1)
const rightDepth = aux(node.right, depth + 1)
return leftDepth < rightDepth ? leftDepth : rightDepth
}
return aux(root, 1)
}