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)


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