133 lines
3.0 KiB
C++
133 lines
3.0 KiB
C++
#include "二叉搜索树.h"
|
|
#include <algorithm>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
// 获取相邻最小偏差值
|
|
int minDiff(TreeNode* root, int min) {
|
|
if (root->left != nullptr) {
|
|
int offset = abs(root->val - root->left->val);
|
|
min = minDiff(root->left, min > offset ? offset : min);
|
|
}
|
|
if (root->right != nullptr) {
|
|
int offset = abs(root->val - root->right->val);
|
|
min = minDiff(root->right, min > offset ? offset : min);
|
|
}
|
|
return min;
|
|
}
|
|
|
|
// 中序二叉树
|
|
void inorder(TreeNode* root, vector<int>& vec) {
|
|
if (root == nullptr)
|
|
return;
|
|
if (root->left != nullptr) {
|
|
inorder(root->left, vec);
|
|
}
|
|
vec.push_back(root->val);
|
|
if (root->right != nullptr) {
|
|
inorder(root->right, vec);
|
|
}
|
|
}
|
|
|
|
int 二叉搜索树::getMinimumDifference(TreeNode* root) {
|
|
vector<int> vec;
|
|
inorder(root, vec);
|
|
int min = INT_MAX;
|
|
for (int i = 1; i < vec.size(); i++) {
|
|
int offset = abs(vec[i] - vec[i - 1]);
|
|
if (min > offset)
|
|
min = offset;
|
|
}
|
|
return min;
|
|
}
|
|
|
|
|
|
void preorder(TreeNode* root, int& max, int& current) {
|
|
current++;
|
|
if (max < current)
|
|
max = current;
|
|
if (root == nullptr)
|
|
return;
|
|
if (root->left != nullptr) {
|
|
preorder(root->left, max, current);
|
|
current--;
|
|
}
|
|
if (root->right != nullptr) {
|
|
preorder(root->right, max, current);
|
|
current--;
|
|
}
|
|
}
|
|
|
|
int 二叉搜索树::maxDepth(TreeNode* root) {
|
|
if (root == nullptr)
|
|
return 0;
|
|
int max = 0, current = 0;
|
|
preorder(root, max, current);
|
|
return max;
|
|
}
|
|
|
|
vector<double> 二叉搜索树::averageOfLevels(TreeNode* root)
|
|
{
|
|
vector<TreeNode*> vec{ root };
|
|
vector<double> ans;
|
|
while (!vec.empty() && vec.size() > 0) {
|
|
vector<TreeNode*> layerVec;
|
|
double value = 0;
|
|
for (int i = 0; i < vec.size(); i++) {
|
|
value += vec[i]->val;
|
|
if (vec[i]->left != nullptr)
|
|
layerVec.push_back(vec[i]->left);
|
|
if (vec[i]->right != nullptr)
|
|
layerVec.push_back(vec[i]->right);
|
|
}
|
|
if (vec.size() > 0)
|
|
ans.push_back(value / vec.size());
|
|
vec = layerVec;
|
|
}
|
|
return ans;
|
|
}
|
|
|
|
vector<int> 二叉搜索树::rightSideView(TreeNode* root) {
|
|
vector<TreeNode*> vec{ root };
|
|
vector<int> ans;
|
|
if (root == nullptr)
|
|
return ans;
|
|
while (!vec.empty() && vec.size() > 0) {
|
|
vector<TreeNode*> layerVec;
|
|
for (int i = 0; i < vec.size(); i++) {
|
|
if (vec[i]->right != nullptr)
|
|
layerVec.push_back(vec[i]->right);
|
|
if (vec[i]->left != nullptr)
|
|
layerVec.push_back(vec[i]->left);
|
|
}
|
|
if (vec.size() > 0)
|
|
ans.push_back(vec[0]->val);
|
|
vec = layerVec;
|
|
}
|
|
return ans;
|
|
}
|
|
|
|
vector<vector<int>> 二叉搜索树::zigzagLevelOrder(TreeNode* root) {
|
|
vector<TreeNode*> vec{ root };
|
|
vector<vector<int>> ans;
|
|
if (root == nullptr)
|
|
return ans;
|
|
int row = 0;
|
|
while (!vec.empty() && vec.size() > 0) {
|
|
vector<TreeNode*> layerVec;
|
|
vector<int> list;
|
|
for (int i = 0; i < vec.size(); i++) {
|
|
list.push_back(vec[i]->val);
|
|
if (vec[i]->right != nullptr)
|
|
layerVec.push_back(vec[i]->right);
|
|
if (vec[i]->left != nullptr)
|
|
layerVec.push_back(vec[i]->left);
|
|
}
|
|
if (row % 2 == 0)
|
|
reverse(list.begin(), list.end());
|
|
ans.push_back(list);
|
|
vec = layerVec;
|
|
row++;
|
|
}
|
|
return ans;
|
|
} |