1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Solution { public: int minDepth(TreeNode* root) { queue<TreeNode*> que; if(root != NULL) que.push(root); int depth = 0; while(!que.empty()) { depth++; int size = que.size(); for(int i = 0; i < size; i++) { TreeNode* node = que.front(); que.pop(); if(node->left) { que.push(node->left); } if(node->right) { que.push(node->right); } if(node->right == nullptr && node->left == nullptr) { return depth; } } } return 0; } };
|