题意:在二叉查找树中查找两个节点之差的最小绝对值
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int ans = 0x3f3f3f3f; TreeNode* pre = NULL; void inorder(TreeNode* root){ if(root == NULL) return; inorder(root -> left); if(pre) ans = min(ans, root -> val - pre -> val); pre = root; inorder(root -> right); } int getMinimumDifference(TreeNode* root) { inorder(root); return ans; } };
原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12609608.html
时间: 2024-09-30 11:14:30