leetcode530. Minimum Absolute Difference in BST

来源:互联网 发布:php播放器源码 编辑:程序博客网 时间:2024/06/05 00:50

平衡二叉树的性质。

中序遍历二叉树为其拍好序列。

/** * 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 fi=1;    int m=2147483647;    int last=0;    void f(TreeNode* root){       if(root==NULL)  return ;        f(root->left);        if(fi){ last=root->val; fi=0; }        else{ m=min(m,abs(last-root->val)); last=root->val; }        f(root->right);    }    int getMinimumDifference(TreeNode* root) {        f(root);        return m;    }};