2023年8月23日 星期三

[129] Sum Root to Leaf Numbers

竟然一次AC !!! 我就說我有進步!!!!!!!(亂吼)
可以回家了 XD (啊才點了超貴的生菜沙啦啊啊啊XD)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int sumLeaves(struct TreeNode* root, int currVal)
{
int left=0, right=0;
if (root->left ==NULL && root->right == NULL)
return currVal*10 + root->val;
if (root->left !=NULL)
left = sumLeaves(root->left, currVal*10+root->val);
if (root->right !=NULL)
right = sumLeaves(root->right, currVal*10+root->val);

return (left+right);
}


int sumNumbers(struct TreeNode* root){
if (root ==NULL)
return 0;
return sumLeaves(root, 0);
}

沒有留言:

張貼留言