NC373. 二叉搜索树最小差值
描述
示例1
输入:
{2,1,3,#,#,#,4}
输出:
1
示例2
输入:
{3,1,5,#,#,#,9}
输出:
2
C++ 解法, 执行用时: 32ms, 内存消耗: 8112KB, 提交时间: 2022-08-06
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int minDifference(TreeNode* root) { // write code here stack<TreeNode*>st; TreeNode* c=root; TreeNode* p=NULL; int r=INT_MAX; while(c||!st.empty()) { if(c) { st.push(c); c=c->left; } else{ c=st.top(); st.pop(); if(p) r=min(r,c->val-p->val); p=c; c=c->right; } } return r; } };
C++ 解法, 执行用时: 33ms, 内存消耗: 8080KB, 提交时间: 2022-05-29
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: int minDifference(TreeNode* root) { stack<TreeNode*>st; TreeNode* c=root; TreeNode* p=NULL; int r=INT_MAX; while(c||!st.empty()) { if(c) { st.push(c); c=c->left; } else { c=st.top(); st.pop(); if(p) r=min(r,c->val-p->val); p=c; c=c->right; } } return r; } };
C++ 解法, 执行用时: 45ms, 内存消耗: 8784KB, 提交时间: 2022-06-04
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int res = 1000000000; int lastVal = 0; int count = 0; int minDifference(TreeNode* root) { travel(root); return res; } void travel(TreeNode* root) { if (root == NULL) { return; } travel(root->left); if (count > 0) { res = root->val - lastVal > res ? res : root->val - lastVal; } count++; lastVal = root->val; travel(root->right); } };
C++ 解法, 执行用时: 45ms, 内存消耗: 8804KB, 提交时间: 2022-06-23
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int ret = INT_MAX; TreeNode* pre =NULL; int minDifference(TreeNode* root) { // write code here // findMinDiff(root, NULL); findMinDiff(root); return ret; } void findMinDiff(TreeNode* root){ if(!root) return; // if(from) ret = min(ret, abs(from->val - root->val)); // pre = root; findMinDiff(root->left); if(pre) { ret = min(ret, abs(pre->val - root->val)); } pre = root; findMinDiff(root->right); } };
C 解法, 执行用时: 45ms, 内存消耗: 8820KB, 提交时间: 2022-07-13
void dfs(struct TreeNode* root, int* f, int* a) { if (root == NULL) { return; } dfs(root->left, f, a); if (*f == -1) { *f = root->val; } else { *a = fmin(*a, root->val - *f); *f = root->val; } dfs(root->right, f, a); } int minDifference(struct TreeNode* root ) { int a = 1000000000; int f = -1; dfs(root, &f, &a); return a; }