计算二叉树的最小深度。最小深度定义为从root到叶子节点的最小路径。
Java版本如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null)return minDepth(root.right) + 1;
if(root.right == null) return minDepth(root.left) + 1;
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}

本文介绍了一种计算二叉树最小深度的方法,并提供了一个Java实现示例。最小深度定义为从根节点到最近叶子节点的最短路径长度。
1670

被折叠的 条评论
为什么被折叠?



