leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0111.cpp (528B)
0 class Solution { 1 public: 2 int minDepth(TreeNode *root) { 3 if (!root) return 0; 4 5 queue<TreeNode *> q; 6 7 q.push(root); 8 for (int lvl = 1; !q.empty(); lvl++) { 9 for (int t = q.size(); t > 0; t--) { 10 TreeNode *root = q.front(); 11 q.pop(); 12 if (!root->left && !root->right) return lvl; 13 if (root->left) q.push(root->left); 14 if (root->right) q.push(root->right); 15 } 16 } 17 return -1; 18 } 19 };