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