Q: seeking a minimum depth of a given binary tree. The minimum depth of the root of the tree is the number of the shortest path to the nearest node of the leaf node.
A binary Tree GIVEN, depth.The Find ITS Minimum Minimum Number of Nodes The depth IS Along The Shortest The path from the root to Down The Nearest Leaf Node Node.
A:
Note that, it must be the root node to the leaf node. So to judge whether or not the root node with children.
public int run(TreeNode root) {
if (root == null)
return 0;
int l = run(root.left);
int r = run(root.right);
return (min(l, r)) + 1;
}
public int min(int a, int b) {
if (a == 0)
return b;
if (b == 0)
return a;
return a < b ? a : b;
}