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