forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
20 lines (20 loc) · 628 Bytes
/
s2.cpp
File metadata and controls
20 lines (20 loc) · 628 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(logN)
// Inorder traversal yields sorted sequence. We just need to
// subtract the previous element from the current one, and keep
// track of the minimum.
// Ref: https://discuss.leetcode.com/topic/80836/c-7-lines-o-n-run-time-o-h-memory
class Solution {
private:
int minVal = INT_MAX, prev = -1;
public:
int getMinimumDifference(TreeNode* root) {
if (!root) return INT_MAX;
getMinimumDifference(root->left);
if (prev != -1) minVal = min(minVal, root->val - prev);
prev = root->val;
getMinimumDifference(root->right);
return minVal;
}
};