标签:解答 init 树节点 amp png for image height ack
题目:
解答:
同题目530。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void inOrder(TreeNode *root, vector<int> &ret) 13 { 14 if (NULL == root) 15 { 16 return; 17 } 18 19 inOrder(root->left, ret); 20 ret.push_back(root->val); 21 inOrder(root->right, ret); 22 } 23 24 int minDiffInBST(TreeNode* root) 25 { 26 int res = INT_MAX; 27 vector<int> ret; 28 vector<int> diff; 29 30 inOrder(root, ret); 31 32 for (int i = 1; i < ret.size(); ++i) 33 { 34 diff.push_back(ret[i] - ret[i-1]); 35 } 36 for (int i = 0; i < diff.size(); ++i) 37 { 38 res = std::min(res, diff[i]); 39 } 40 41 return res; 42 43 } 44 };
标签:解答 init 树节点 amp png for image height ack
原文地址:https://www.cnblogs.com/ocpc/p/12822043.html