0111.cpp (528B)
1 class Solution { 2 public: 3 int minDepth(TreeNode *root) { 4 if (!root) return 0; 5 6 queue<TreeNode *> q; 7 8 q.push(root); 9 for (int lvl = 1; !q.empty(); lvl++) { 10 for (int t = q.size(); t > 0; t--) { 11 TreeNode *root = q.front(); 12 q.pop(); 13 if (!root->left && !root->right) return lvl; 14 if (root->left) q.push(root->left); 15 if (root->right) q.push(root->right); 16 } 17 } 18 return -1; 19 } 20 };