2019.4.3 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)
本期开始更LeetCode,因为博客速度已经远远赶不上刷题速度,因此考虑只把比较有价值的题目放上博客作为记录。
深度就是按层计数的结点数,一个结点的深度是1。
二叉树的最小深度可以依靠剪枝层序遍历,也就是从根节点开始进行BFS,当遇到第一个叶子结点的时候停止,此时的深度对应着最小深度。
二叉树的最大深度只需要进行简单的递归,每个结点的最大深度等于左子树的最大深度与右子树的最大深度的较大值+1。
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.
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最小深度 2.
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no childr