【力扣】111、二叉树的最小深度

111、二叉树的最小深度

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。
在这里插入图片描述

// 
var minDepth = function(root){
	if(!root) return 0;
	const stack = [ [ root ,1] ];//放置整个节点;
	while(stack.length){
		const [o,n] = stack.shift();//把数组的第一个元素从其中删除,并返回第一个元素的值;
		if(!o.left && !o.right){
			return n;
		}
		if(o.left) stack.push([o.left,n+1]);
		if(o.right) stack.push([o.right,n+1]);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44806635/article/details/131530633