https://leetcode.com/problems/minimum-depth-of-binary-tree/
找到树的最小深度
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 1;
}
// 对于所有跟“叶子节点”有关的问题,都一定要注意处理当前节点“只有”左子节点或者“只有”右子节点的情况
return Math.min(root.left == null ? Integer.MAX_VALUE : minDepth(root.left), root.right == null ? Integer.MAX_VALUE : minDepth(root.right)) + 1;
}
}
转载请注明原文地址: https://ju.6miu.com/read-659603.html