leetcode

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

1026.cpp (626B)


      1 class Solution {
      2   public:
      3     int maxAncestorDiff(TreeNode *root) {
      4         if (!root) return 0;
      5         int res = 0;
      6         stack<tuple<TreeNode *, int, int>> st;
      7         st.push({root, root->val, root->val});
      8         while (!st.empty()) {
      9             auto [root, maxi, mini] = st.top();
     10             st.pop();
     11             res = max({res, abs(maxi - root->val), abs(mini - root->val)});
     12             maxi = max(maxi, root->val), mini = min(mini, root->val);
     13             if (root->left) st.push({root->left, maxi, mini});
     14             if (root->right) st.push({root->right, maxi, mini});
     15         }
     16         return res;
     17     }
     18 };