/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode *root) {
if(root == NULL){
return 0;
}
long long ret = 0;
queue<TreeNode*> tnq;
tnq.push(root);
//tnq.push(NULL);
while(!tnq.empty()){
TreeNode* tmp = tnq.front();
tnq.pop();
bool isleaf = true;
if(tmp->left){
isleaf = false;
//记得tnq里的变量类型是什么,不要搞错了
tmp->left->val = tmp->val * 10 + tmp->left->val;
tnq.push(tmp->left);
}
if(tmp->right){
isleaf = false;
tmp->right->val = tmp->val * 10 + tmp->right->val;
tnq.push(tmp->right);
}
if(isleaf){
ret += tmp->val;
}
}
return ret;
}
};
leetcode sum root to leaf
最新推荐文章于 2020-06-11 01:48:15 发布