Created
November 10, 2018 11:24
-
-
Save fpdjsns/594442e85df0096e7b2f86f5fb120c15 to your computer and use it in GitHub Desktop.
[leetcode] 129. Sum Root to Leaf Numbers : https://leetcode.com/problems/sum-root-to-leaf-numbers/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
int ans = 0; | |
void setAns(TreeNode* root, int sum){ | |
if(root == NULL) return; | |
sum += root->val; | |
if(root->left == NULL && root->right ==NULL) | |
ans += sum; | |
sum *= 10; | |
setAns(root->left, sum); | |
setAns(root->right, sum); | |
} | |
int sumNumbers(TreeNode* root) { | |
ans = 0; | |
setAns(root, 0); | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment