0783.cpp (539B)
1 class Solution { 2 public: 3 int minDiffInBST(TreeNode *root) { 4 stack<TreeNode *> st; 5 int res = INT_MAX; 6 TreeNode *prev = new TreeNode(INT_MAX); 7 while (true) { 8 while (root) { 9 st.push(root); 10 root = root->left; 11 } 12 if (st.empty()) break; 13 root = st.top(); 14 st.pop(); 15 res = min(res, abs(prev->val - root->val)); 16 prev = root; 17 root = root->right; 18 } 19 return res; 20 } 21 };