/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* pre = NULL; int result = INT_MAX; //中序遍历是递增的 int getMinimumDifference(TreeNode* root) { if(root == NULL) return -1; getMinimumDifference(root->left); if(pre != NULL){ result = min(result, abs(pre->val - root->val)); } pre = root; getMinimumDifference(root->right); return result; } };
标签:right,TreeNode,val,int,day21,part07,二叉树,root,left From: https://www.cnblogs.com/zqh2023/p/17679400.html