leetcode 111. Minimum Depth of Binary Tree

来源:互联网 发布:爱奇艺点击率换算法 编辑:程序博客网 时间:2024/04/29 12:48
题目:

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.

求二叉树的最小深度,二叉树的最小深度为根节点到最近叶子节点的距离。


与二叉树的最大深度不同,因为二叉树的最小深度必须是根结点到叶子结点的距离,不能单纯的比较左右子树的递归结果返回较小值,因为对于有单个孩子为空的节点,为空的孩子会返回0,但这个节点并非叶子节点,故返回的结果是错误的。

因此,当发现当前处理的节点有单个孩子是空时,返回一个极大值Integer.MAX_VALUE,防止其干扰结果。

代码AC:

0 0