. - 力扣(LeetCode)
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void dfs(TreeNode* root, int& pre, int& ans) {if (root == nullptr) {return;}dfs(root->left, pre, ans);if (pre == INT_MIN) {pre = root->val;} else {ans = min(ans, root->val - pre);pre = root->val;}dfs(root->right, pre, ans);}int getMinimumDifference(TreeNode* root) {int ans = INT_MAX, pre = INT_MIN;dfs(root, pre, ans);return ans;}
};