s4553711
7/18/2017 - 2:26 PM

530.cpp

/**
 * 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 min = INT_MAX;
    int prev = -1;
    int getMinimumDifference(TreeNode* root) {
        if (root == NULL) return min;
        getMinimumDifference(root->left);
        if (prev != -1) {
            min = std::min(min, root->val - prev);
        }
        prev = root->val;
        getMinimumDifference(root->right);
        return min;
    }
};