LeetCode111. Minimum Depth of Binary Tree
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.
(补昨天沉迷小说未写的题解
(不写题解的刷题是不完整的!
好了,题目是说求二叉树的最小高度,其定义是根节点到最近的叶结点的高度。
我们知道,每个结点都有好几种情况,1是这个结点就是空的,返回0;2是这个结点既没有左结点也没有右节点,返回1;3是这个结点只有左子树,那么应该返回左子树的最小高;4是这个结点只有右子树,则返回右子树的最小高;最后,如果这个结点既有左子树又有右子树,返回二者高度小的那个。只有最后一步需要递归,其他的简单的判断一下就好了。