leetcode

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

0889.cpp (502B)


      1 class Solution {
      2   public:
      3     TreeNode *constructFromPrePost(const vector<int> &pre, const vector<int> &post) {
      4         vector<TreeNode *> s = {new TreeNode(pre[0])};
      5         for (int i = 1, j = 0; i < pre.size(); i++) {
      6             TreeNode *node = new TreeNode(pre[i]);
      7             while (s.back()->val == post[j])
      8                 s.pop_back(), j++;
      9             (!s.back()->left ? s.back()->left : s.back()->right) = node;
     10             s.push_back(node);
     11         }
     12         return s.front();
     13     }
     14 };