#include <lintcode.h>
◆ minDiffInBST()
int lintcode::min_diff_in_BST::Solution::minDiffInBST |
( |
TreeNode * |
root | ) |
|
|
static |
- 参数
-
root | a Binary Search Tree (BST) with the root node |
- 返回
- : the minimum difference
在文件 lintcode.cpp 第 298 行定义.
299 auto vals = vector<int>();
300 auto que = queue<TreeNode *>();
302 while(!que.empty()) {
303 const auto *node = que.front();
305 vals.push_back(node->val);
306 if(node->left !=
nullptr) {
307 que.push(node->left);
309 if(node->right !=
nullptr) {
310 que.push(node->right);
313 sort(vals.begin(), vals.end());
314 int minimum = INT_MAX;
315 for(
int i = 0; i + 1 < vals.size(); i++) {
316 minimum = min(vals[i + 1] - vals[i], minimum);
引用了 acwing::acwing836_408::root.
该类的文档由以下文件生成: