-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path783.cpp
More file actions
28 lines (27 loc) · 682 Bytes
/
Copy path783.cpp
File metadata and controls
28 lines (27 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
int min_diff;
public:
int minDiffInBST(TreeNode *root) {
min_diff = INT_MAX;
Traverse(root);
return min_diff;
}
void Traverse(TreeNode *node) {
if (!node)
return;
if (node->left) {
auto s = node->left;
while (s->right)
s = s->right;
min_diff = min(min_diff, node->val - s->val);
}
if (node->right) {
auto s = node->right;
while (s->left)
s = s->left;
min_diff = min(min_diff, s->val - node->val);
}
Traverse(node->left);
Traverse(node->right);
}
};