LeetCode 530. Minimum Absolute Difference in BST
程序员文章站
2022-03-07 18:12:42
...
530.Minimum Absolute Difference in BST
解析
该面壁的代码。晚上二刷。
https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
class Solution {
public:
int q(int x){
return x>0?x:-x;
}
int getMinimumDifference(TreeNode* root) {
int* path = new int[10000];
int len=0;
travel(root,path,len);
int min = path[1]-path[0];
for(int i=1;i<len;i++){
int diff = path[i]-path[i-1];
min = min>diff ? diff:min;
}
return min;
}
void travel(TreeNode* root,int* path,int& len){
if(root == NULL)
return;
travel(root->left,path,len);
path[len++] =root->val;
travel(root->right,path,len);
}
};
推荐阅读
-
LeetCode --- 783. Minimum Distance Between BST Nodes 解题分析
-
LeetCode --- 783. Minimum Distance Between BST Nodes 解题分析
-
530. Minimum Absolute Difference in BST
-
Leetcode每日一题:530.minimum-absolute-difference-in-bst(二叉搜索树的最小绝对值)
-
530. Minimum Absolute Difference in BST
-
LC-Minimum Absolute Difference in BST
-
(Java)leetcode-530 Minimum Absolute Difference in BST (二叉搜索树的最小绝对差)
-
LeetCode之路:530. Minimum Absolute Difference in BST
-
530 - 二叉搜索树的最小绝对差(minimum-absolute-difference-in-bst)
-
530. Minimum Absolute Difference in BST