32 - 二叉树的最小深度
题目
解答
深度优先
var minDepth = function (root) {
if (!root) {
return 0
}
if (!root.left && !root.right) {
return 1
}
let depth = Number.MAX_SAFE_INTEGER
if (root.left) {
depth = Math.min(minDepth(root.left), depth);
}
if (root.right) {
depth = Math.min(minDepth(root.right), depth);
}
return depth + 1
};广度优先
Last updated