problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
lintcode::min_diff_in_BST::Solution类 参考

#include <lintcode.h>

静态 Public 成员函数

static int minDiffInBST (TreeNode *root)
 

详细描述

在文件 lintcode.h157 行定义.

成员函数说明

◆ minDiffInBST()

int lintcode::min_diff_in_BST::Solution::minDiffInBST ( TreeNode root)
static
参数
roota Binary Search Tree (BST) with the root node
返回
: the minimum difference

在文件 lintcode.cpp298 行定义.

298 {
299 auto vals = vector<int>();
300 auto que = queue<TreeNode *>();
301 que.push(root);
302 while(!que.empty()) {
303 const auto *node = que.front();
304 que.pop();
305 vals.push_back(node->val);
306 if(node->left != nullptr) {
307 que.push(node->left);
308 }
309 if(node->right != nullptr) {
310 que.push(node->right);
311 }
312 }
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);
317 }
318 return minimum;
319 }
vector< int > root
Definition: acwing408.cpp:349

引用了 acwing::acwing836_408::root.


该类的文档由以下文件生成: