leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0129.cpp (644B)


      1 class Solution {
      2   public:
      3     int sumNumbers(TreeNode *root) {
      4         if (!root) return 0;
      5 
      6         int sum = 0, res = 0;
      7         stack<pair<TreeNode *, int>> st;
      8         while (true) {
      9             while (root) {
     10                 sum *= 10;
     11                 sum += root->val;
     12                 if (root->right)
     13                     st.push({root->right, sum});
     14                 else if (!root->left)
     15                     res += sum;
     16                 root = root->left;
     17             }
     18             if (st.empty()) break;
     19             root = st.top().first;
     20             sum = st.top().second;
     21             st.pop();
     22         }
     23 
     24         return res;
     25     }
     26 };