题目:
题解:
int sumNumbers(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
int sum = 0;
struct TreeNode* nodeQueue[2000];
int numQueue[2000];
int leftQueue = 0, rightQueue = 0;
nodeQueue[rightQueue] = root;
numQueue[rightQueue++] = root->val;
while (leftQueue < rightQueue) {
struct TreeNode* node = nodeQueue[leftQueue];
int num = numQueue[leftQueue++];
struct TreeNode* left = node->left;
struct TreeNode* right = node->right;
if (left == NULL && right == NULL) {
sum += num;
} else {
if (left != NULL) {
nodeQueue[rightQueue] = left;
numQueue[rightQueue++] = num * 10 + left->val;
}
if (right != NULL) {
nodeQueue[rightQueue] = right;
numQueue[rightQueue++] = num * 10 + right->val;
}
}
}
return sum;
}