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.
1 class Solution{ 2 public: 3 int minDepth(TreeNode *root){ 4 if(root==NULL) 5 { 6 return 0; 7 } 8 if(root->left==NULL&&root->right==NULL) 9 { 10 return 1; 11 } 12 else 13 { 14 int a=minDepth(root->left); 15 int b=minDepth(root->right); 16 if(a==0||b==0) 17 { 18 return (a>b?a:b)+1; 19 } 20 else 21 { 22 return (a<b?a:b)+1; 23 } 24 } 25 } 26 };