530.二叉搜索树的最小绝对差
class Solution {
public:
// 求最小公共祖先,需要从底向上遍历,那么二叉树,只能通过后序遍历(即:回溯)实现从底向上的遍历方式。
// 在回溯的过程中,必然要遍历整棵二叉树,即使已经找到结果了,依然要把其他节点遍历完,因为要使用递归函数的返回值(也就是代码中的left和right)做逻辑判断。
// 要理解如果返回值left为空,right不为空为什么要返回right,为什么可以用返回right传给上一层结果。
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == q || root == p || root == NULL) {
return root;
}
// 后序遍历是天然的回溯
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left != NULL && right != NULL) {
return root;
}
if (left != NULL && right == NULL) {
return left;
} else if (left == NULL && right != NULL) {
return right;
} else {
// 左右都为空
return NULL;
}
}
};
501.二叉搜索树中的众数
class BiTreeSolution {
private:
// 前序遍历统计元素和频数
void searchBST(TreeNode* cur, unordered_map<int, int>& map) {
if (cur == NULL) {
return;
}
map[cur->val]++;
searchBST(cur->left, map);
searchBST(cur->right, map);
return;
}
// 自定义的比较函数,在C++中,静态成员函数可以在没有类对象的情况下调用,它没有访问 this 指针的权限。通过将 cmp 声明为静态,可以在不需要 Solution 类的实例的情况下使用它作为比较函数。
//由于 cmp 函数不依赖于任何实例特定的数据或行为,将其声明为静态可以提高代码的可读性,并避免为对象分配不必要的内存。
bool static cmp(const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
}
public:
vector<int> findMode(TreeNode* root) {
unordered_map<int, int> map;
vector<int> result;
searchBST(root, map);
vector<pair<int, int>> vec(map.begin(), map.end());
sort(vec.begin(), vec.end(), cmp);
result.push_back(vec[0].first);
for (int i = 1; i < vec.size(); ++i) {
if (vec[i].second == vec[0].second) {
result.push_back(vec[i].first);
} else break;
}
return result;
}
};
class Solution {
private:
int maxCount = 0;
int count = 0;
TreeNode* pre;
vector<int> result;
void searchBST(TreeNode* cur) {
if (cur == NULL) {
return;
}
searchBST(cur->left); // 左
if (pre == NULL) { // 根
// 前指针为空,为第一个节点
count = 1;
} else if (pre->val == cur->val) {
// 前后节点相同
count++;
} else {
// 前后节点不同
count = 1;
}
// 更新前指针
pre = cur;
// 计数相关
if (maxCount == count) {
result.push_back(cur->val);
}
if (count > maxCount) {
result.clear();
maxCount = count;
result.push_back(cur->val);
}
// 右
searchBST(cur->right);
return;
}
public:
vector<int> findMode(TreeNode* root) {
count = 0;
maxCount = 0;
TreeNode* pre = NULL;
result.clear();
searchBST(root);
return result;
}
// 迭代法
vector<int> findMode1(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
int maxCount = 0; // 最大频率
int count = 0; // 统计频率
vector<int> result;
while (cur != NULL || !st.empty()) {
if (cur != NULL) { // 指针来访问节点,访问到最底层
st.push(cur); // 将访问的节点放进栈
cur = cur->left; // 左
} else {
cur = st.top();
st.pop(); // 中
if (pre == NULL) { // 第一个节点
count = 1;
} else if (pre->val == cur->val) { // 与前一个节点数值相同
count++;
} else { // 与前一个节点数值不同
count = 1;
}
if (count == maxCount) { // 如果和最大值相同,放进result中
result.push_back(cur->val);
}
if (count > maxCount) { // 如果计数大于最大值频率
maxCount = count; // 更新最大频率
result.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
result.push_back(cur->val);
}
pre = cur;
cur = cur->right; // 右
}
}
return result;
}
};
236. 二叉树的最近公共祖先
归纳如下三点:
- 求最小公共祖先,需要从底向上遍历,那么二叉树,只能通过后序遍历(即:回溯)实现从底向上的遍历方式。
- 在回溯的过程中,必然要遍历整棵二叉树,即使已经找到结果了,依然要把其他节点遍历完,因为要使用递归函数的返回值(也就是代码中的left和right)做逻辑判断。
- 要理解如果返回值left为空,right不为空为什么要返回right,为什么可以用返回right传给上一层结果。
class Solution {
public:
// 很妙的性质+中序遍历递归
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) {
return;
}
traversal(root->left);
vec.push_back(root->val);
traversal(root->right);
}
int getMinimumDifference(TreeNode* root) {
vec.clear();
traversal(root);
// 个数小于2,无法比较,题目中指明了节点个数大于2
// if (vec.size() < 2) {
// return 0;
// }
int result = INT16_MAX;
for (int i = 1; i < vec.size(); ++i) {
// 二叉搜索树性质,中序遍历的结果是一个单调递增的数组
result = min(result, vec[i] - vec[i - 1] );
}
return result;
}
// 不那么妙的指针+中序遍历递归
int result = INT32_MAX;
TreeNode* pre = NULL;
void traversal1(TreeNode* cur) {
// 终止条件
if (cur == NULL) {
return;
}
// 左
traversal1(cur->left);
// 根
if (pre != NULL) {
result = min(result, cur->val - pre->val);
}
pre = cur;
// 右
traversal1(cur->right);
}
int getMinimumDifference1(TreeNode* root) {
traversal1(root);
return result;
}
// 中序遍历迭代法
int getMinimumDifference2(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
int result = INT32_MAX;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left; // 左
} else {
cur = st.top();
st.pop();
if (pre != NULL) {
result = min(result, cur->val - pre->val); // 根
}
pre = cur;
cur = cur->right; // 右
}
}
return result;
}
};