LeetCode——Minimum Depth of Binary Tree

来源:互联网 发布:淘宝名字大全英文名 编辑:程序博客网 时间:2024/05/01 16:18

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.

中文:给一二叉树,找出其最小深度。

最小深度是从根节点到最近的叶子节点的最短路径经过的节点的数量。

Java:

public int minDepth(TreeNode root) {if (root == null)return 0;int a = minDepth(root.left);int b = minDepth(root.right);if (a == 0)return b + 1;if (b == 0)return a + 1;return Math.min(a, b) + 1;}


0 0