-
Notifications
You must be signed in to change notification settings - Fork 0
/
129.sum-root-to-leaf-numbers.cpp
50 lines (45 loc) · 1.25 KB
/
129.sum-root-to-leaf-numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode id=129 lang=cpp
*
* [129] Sum Root to Leaf Numbers
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
int sum = 0;
stack<pair<TreeNode*, int>> s;
if (!root)return 0;
s.push(make_pair(root, root->val));
TreeNode* curNode;
int curVal;
while(!s.empty()){
curNode = s.top().first;
curVal = s.top().second;
s.pop();
if (!curNode->left && !curNode->right){
sum += curVal;
continue;
}
if (curNode->left){
s.push(make_pair(curNode->left, curVal * 10 + curNode->left->val));
}
if (curNode->right){
s.push(make_pair(curNode->right, curVal * 10 + curNode->right->val));
}
}
return sum;
}
};
// @lc code=end