1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public: int maxDepth(TreeNode* root) { if (root == nullptr) return 0; int height = 0; queue<TreeNode*> q; q.push(root); while (!q.empty()) { height++; int n = q.size(); for (int i = 0; i < n; i++) { TreeNode* temp = q.front(); q.pop(); if (temp->left != nullptr) q.push(temp->left); if (temp->right != nullptr) q.push(temp->right); } }
return height; } };
|