Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. const int64_t INF = numeric_limits::max();
  11.  
  12. class Solution {
  13. public:
  14. int minDiffInBST(TreeNode* root) {
  15. int64_t min_diff = INF;
  16. int64_t pred_val = INF;
  17. minDiff(root, pred_val, min_diff);
  18.  
  19.  
  20. //min_diff = min( min_diff, abs(prev_val -
  21.  
  22. return min_diff;
  23. }
  24.  
  25. private:
  26. void minDiff(TreeNode* root, int64_t& pred_val, int64_t& min_diff) {
  27. if (!root) return ;
  28.  
  29. minDiff(root->left, pred_val, min_diff);
  30. min_diff = min(min_diff, abs(pred_val - root->val));
  31. pred_val = root->val;
  32. minDiff(root->right, pred_val, min_diff);
  33. }
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement