题目描述
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.
import java.util.*;
public class Solution {
public int run(TreeNode root) {
if( root == null) return 0;
if( root != null && root.left == null && root.right == null) return 1;
if( root.left == null ) return run( root.right)+1;
if( root.right == null ) return run( root.left)+1;
return Math.min(run(root.left),run(root.right))+1;
}
}

本文介绍了一种求解二叉树最小深度的递归算法。该算法通过遍历二叉树并找到从根节点到最近叶子节点的最短路径长度来实现。文章中的Java代码实现了这一逻辑。
142

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



