aboutsummaryrefslogtreecommitdiff
path: root/deepest_leaves_sum.cpp
blob: bb84f4c8f9f889ee46cb51d4f1cd4c8db916483b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "leetcode.h"

class Solution {
public:
  int deepestLeavesSum(TreeNode *root) {
    int ans = 0, i;
    queue<TreeNode *> q;
    q.push(root);
    while (q.size()) {
      for (i = q.size() - 1, ans = 0; i >= 0; --i) {
        TreeNode *node = q.front();
        q.pop();
        ans += node->val;
        if (node->right)
          q.push(node->right);
        if (node->left)
          q.push(node->left);
      }
    }
    return ans;
  }
};

int main() {}