2023每日刷题(五十八)
Leetcode—530.二叉搜索树的最小绝对差
实现代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void dfs(struct TreeNode* root, int* pre, int* ans) {
if(root == NULL) {
return;
}
dfs(root->left, pre, ans);
if(*pre == -1) {
*pre = root->val;
} else {
*ans = fmin(*ans, root->val - *pre);
*pre = root->val;
}
dfs(root->right, pre, ans);
}
int getMinimumDifference(struct TreeNode* root) {
int ans = 1e5+5;
int pre = -1;
dfs(root, &pre, &ans);
return ans;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!