參考資訊:
https://www.cnblogs.com/grandyang/p/15022629.html
題目:
解答:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int maxLevelSum(struct TreeNode *root) { int r = 0; int st = 0; int ep = 0; int max = 0; int level = 0; struct TreeNode *buf[10000] = { 0 }; if (!root) { return 0; } r = 1; buf[ep++] = root; max = root->val; while ((ep - st) > 0) { int cnt = 0; int cc = 0; int s0 = st; int s1 = ep; for (cc = s0; cc < s1; cc++) { st += 1; cnt += buf[cc]->val; if (buf[cc]->left) { buf[ep++] = buf[cc]->left; } if (buf[cc]->right) { buf[ep++] = buf[cc]->right; } } level += 1; if (cnt > max) { max = cnt; r = level; } } return r; }