标题
530.二叉搜索树的最小绝对差
自己审题思路
利用二叉搜索树特性,保存前一个节点
看完代码随想录题解后的收获
基本一致
代码(递归1):
class Solution {
public:
int minValue = INT_MAX;
TreeNode* pre = nullptr;
int getMinimumDifference(TreeNode* root) {
if (root == nullptr) return minValue;
getMinimumDifference(root->left);
if(pre != nullptr && root->val - pre->val < minValue) minValue = root->val - pre->val;
pre = root;
getMinimumDifference(root->right);
return minValue;
}
};
代码(递归2)
class Solution {
private:
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
vec.push_back(root->val); // 将二叉搜索树转换为有序数组
traversal(root->right);
}
public:
int getMinimumDifference(TreeNode* root) {
vec.clear();
traversal(root);
if (vec.size() < 2) return 0;
int result = INT_MAX;
for (int i = 1; i < vec.size(); i++) { // 统计有序数组的最小差值
result = min(result, vec[i] - vec[i-1]);
}
return result;
}
};
最直观想法:将二叉搜索树转化为有序数组,最终统计有序数组最小差值
代码(迭代):
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
stack<TreeNode*> st;
int result = INT_MAX;
if (root == nullptr) return 0;
TreeNode* cur = root;
TreeNode* pre = nullptr;
while (!st.empty() || cur != nullptr) {
while (cur != nullptr) {
st.push(cur);
cur = cur->left;
}
cur = st.top(); st.pop();
if (pre != nullptr) {
result = min(result, cur->val - pre->val);
}
pre = cur;
cur = cur->right;
}
return result;
}
};
参考详解
501.二叉搜索树中的众数
自己审题思路
遍历二叉树后将元素和出现的频率放在map中,之后调出频率最大值输出。
看完代码随想录题解后的收获
二叉搜索树特性完成本题。
代码(map存储二叉树节点值与出现频率1):
class Solution {
public:
unordered_map<int,int> ans;
void traversal(TreeNode* root) {
if (root == nullptr) return;
ans[root->val]++;
traversal(root->left);
traversal(root->right);
return;
}
vector<int> findMode(TreeNode* root) {
vector<int> result;
ans.clear();
traversal(root);
int maxFreq = 0;
cout << "---------------------" << endl;
for(auto it = ans.begin(); it != ans.end(); it++) {
cout<< it->first <<" "<< it->second << endl;
}
cout << "---------------------" << endl;
for(auto it = ans.begin(); it != ans.end();it++){
if(it->second > maxFreq) {
result.clear();
result.push_back(it->first);
maxFreq = it->second;
} else if(it->second == maxFreq) {
cout << it->second << endl;
result.push_back(it->first);
}
}
return result;
}
};
代码(map存储二叉树节点值与出现频率2):
class Solution {
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 ;
}
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; // key:元素,value:出现频率
vector<int> result;
if (root == NULL) return 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++) {
// 取最高的放到result数组中
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 = NULL;
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 (count == maxCount) { // 如果和最大值相同,放进result中
result.push_back(cur->val);
}
if (count > maxCount) { // 如果计数大于最大值频率
maxCount = count; // 更新最大频率
result.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
result.push_back(cur->val);
}
searchBST(cur->right); // 右
return ;
}
public:
vector<int> findMode(TreeNode* root) {
count = 0;
maxCount = 0;
pre = NULL; // 记录前一个节点
result.clear();
searchBST(root);
return result;
}
};
代码(迭代):
class Solution {
public:
vector<int> findMode(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. 二叉树的最近公共祖先
自己审题思路
自底向上遍历
看完代码随想录题解后的收获
自底向上遍历,选择后序遍历
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left != NULL && right != NULL) return root;
else if (left != NULL && right == NULL) return left;
else if (left == NULL && right != NULL) return right;
else return NULL;
}
};