1. 程式人生 > >Leetcode-5017 Sum of Root To Leaf Binary Numbers(從根到葉的二進制數之和)

Leetcode-5017 Sum of Root To Leaf Binary Numbers(從根到葉的二進制數之和)

void leaf int ret number ber numbers num bin

 1 typedef long long ll;
 2 class Solution
 3 {
 4     public:
 5         ll rnt = 0;
 6         void go(TreeNode* root,int val)
 7         {
 8             val = (val*2+root->val)%1000000007;
 9             if(root->left==NULL && root->right==NULL)
10             {
11                 rnt = (rnt+val)%1000000007
;return ; 12 } 13 if(root->left) 14 go(root->left,val); 15 if(root->right) 16 go(root->right,val); 17 } 18 int sumRootToLeaf(TreeNode* root) 19 { 20 if(!root) return 0; 21 go(root,0
); 22 return rnt; 23 } 24 };

沒取余WA了兩發,氣死我了

Leetcode-5017 Sum of Root To Leaf Binary Numbers(從根到葉的二進制數之和)