首页 > 其他 > 详细

【easy】530. Minimum Absolute Difference in BST

时间:2018-02-12 18:10:24      阅读:236      评论:0      收藏:0      [点我收藏+]

找BST树中节点之间的最小差值。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
//BST树中序遍历就是递增的序列,相邻两个数的差值,找最小
class Solution {
public:
    int min_res = INT_MAX;
    TreeNode* pre;
    
    int getMinimumDifference(TreeNode* root) {
        helper(root);
        return min_res;
    }
    
    void helper(TreeNode*root){
        if (!root)
            return;
        helper(root->left);  //1
        
        if (pre)
            min_res = min(min_res, abs(root->val - pre->val));
        pre = root;
        
        helper(root->right);  //2
    }
};

 

【easy】530. Minimum Absolute Difference in BST

原文:https://www.cnblogs.com/sherry-yang/p/8444912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!