leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0129.cpp (644B)
0 class Solution {
1 public:
2 int sumNumbers(TreeNode *root) {
3 if (!root) return 0;
5 int sum = 0, res = 0;
6 stack<pair<TreeNode *, int>> st;
7 while (true) {
8 while (root) {
9 sum *= 10;
10 sum += root->val;
11 if (root->right)
12 st.push({root->right, sum});
13 else if (!root->left)
14 res += sum;
15 root = root->left;
16 }
17 if (st.empty()) break;
18 root = st.top().first;
19 sum = st.top().second;
20 st.pop();
21 }
23 return res;
24 }
25 };