leetCode530

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36257146/article/details/102724754

中序遍历

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

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/102724754