标签:div imp node mini queue dep link treenode out
import java.util.* class Solution { fun minDepth(root: TreeNode?): Int { if (root == null) { return 0 } var depth = 0 //LinkedList实现了Queue接口,可以用作队列使用 val queue = LinkedList<TreeNode>() queue.offer(root) while (queue.size > 0) { depth++ val size = queue.size for (i in size - 1 downTo 0) { val node = queue.poll() //if found out the first leaf, return the depth //leaf is a node with no children if (node.left == null && node.right == null) { return depth } if (node.left != null) { queue.offer(node.left) } if (node.right != null) { queue.offer(node.right) } } } return -1 } }
111. Minimum Depth of Binary Tree
标签:div imp node mini queue dep link treenode out
原文地址:https://www.cnblogs.com/johnnyzhao/p/12712819.html