葉林
Linked with GitHub
# LeetCode - 0530. Minimum Absolute Difference in BST ### 題目網址:https://leetcode.com/problems/minimum-absolute-difference-in-bst/ ###### tags: `LeetCode` `Easy` `圖論` ```cpp= /* -LeetCode format- Problem: 530. Minimum Absolute Difference in BST Difficulty: Easy by Inversionpeter */ /** * 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 { int previous = -1, minimum = 2147483647; void InorderTraversal(TreeNode *node) { if (!node) return; InorderTraversal(node->left); if (previous != -1) minimum = min(minimum, abs(previous - node->val)); previous = node->val; InorderTraversal(node->right); } public: int getMinimumDifference(TreeNode* root) { InorderTraversal(root); return minimum; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up