LeetCode lc111.二叉树的最小深度
作者:互联网
1. 题目地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/submissions/
2. 题目解析:【1】使用广度优先搜索BFS,对于每一层的节点,如果第一次发现某节点左、右子结点均为空,则此节点为二叉树的最小深度.
var minDepth = function(root) { if (!root) { return 0; } let queen = [root]; let ans = 0; let node = null; while (queen.length) { ans++; let q_size = queen.length; while (q_size--) { node = queen.shift(); if (node.left) { queen.push(node.left); } if (node.right) { queen.push(node.right); } if (!node.left && !node.right) { return ans; } } } };
【2】使用 递归深度优先搜索DFS,
var minDepth = function(root) { if (root != null) { if (!root.left && !root.right) { return 1; } let mins = 10001; if (root.left) { mins = Math.min(minDepth(root.left), mins); } if (root.right) { mins = Math.min(minDepth(root.right), mins); } return mins + 1; } return 0; };
标签:node,lc111,queen,right,二叉树,mins,root,LeetCode,left 来源: https://www.cnblogs.com/twinkleG/p/15345470.html