leetcode-Minimum Depth of Binary Tree

 1 class Solution {
 2 public:
 3     int minDepth(TreeNode *root) {
 4         if (root == nullptr) return 0;
 5         if (root->left != nullptr&&root->right != nullptr)
 6         {
 7             int lDepth = minDepth(root->left);
 8             int rDepth = minDepth(root->right);
 9             return (lDepth<rDepth?lDepth+1:rDepth+1);
10         }
11         if (root->left != nullptr)
12         {
13             return 1+minDepth(root->left);
14         }
15         if (root->right != nullptr)
16         {
17             return 1+minDepth(root->right);
18         }
19         return 1;//只有一个根节点时。
20     }
21 };
原文地址:https://www.cnblogs.com/forcheryl/p/4058889.html