今日内容
- 530.二叉搜索树的最小绝对差
- 501.二叉搜索树中的众数
- 236. 二叉树的最近公共祖先
就三道题,看上去就不简单(狗头)
一、二叉搜索树的最小绝对差
这题有很多方法,可以和昨天一样,把它变成一个有序数组,然后遍历数组找最小差值。
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);
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;
}
};
第二种没有形成数组,而是在递归的途中判断当前节点和前一个递归到的节点的差值。
迭代写法:
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur =root;
TreeNode* pre =NULL;
int result =INT_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;
}
};
大致思路是先把所有的左侧节点推进栈中,当到头时,从栈中退出最左侧节点,作为最小值,进行比较右侧节点。
二、二叉搜索树中的众数
这题题解首先给出了最普通的思路,就是如果此题不是二叉搜索树而是一棵普通树,就需要用哈希将元素与其对应的次数统计出来,然后依据频率进行排序(此处是用到了pair,将map中的数据提取出来。
sort(vec.begin(), vec.end(), cmp);
这里的三个参数分别是起始位置,终止位置,和判定条件(条件在上方cmp中,越大的值排在最前)
最后将相同频率的数输出即可。
class Solution {
public:
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;
}
vector<int> findMode(TreeNode* root){
unordered_map<int,int> map;
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++){
if(vec[i].second==vec[0].second) result.push_back(vec[i].first);
else{
break;
}
}
return result;
}
};
而本题给出的是二叉搜索树,所以他的中序遍历就是有序的
在题解上给出有pre的时候,就基本上心里有谱了。
class Solution {
public:
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.push_back(cur->val);
}
if(count>maxcount){
maxcount=count;
result.clear();
result.push_back(cur->val);
}
searchBST(cur->right);
return;
}
vector<int> findMode(TreeNode* root) {
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;
}
};
三、二叉树的最近公共祖先
这题还是需要用到回溯法
1、确定递归函数的返回值和参数
需要从root开始,需要找到p节点和q节点,返回值则为节点类型
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
2、确定终止条件
如果 root == q,或者 root == p,说明找到 q p ,则将其返回
if (root == q || root == p || root == NULL) return root;
3、单层递归逻辑
if (left == NULL && right != NULL) return right;
else if (left != NULL && right == NULL) return left;
else { // (left == NULL && right == NULL)
return NULL;
}
整体代码
/**
* 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==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 right;
else if(left!=NULL&&right==NULL)
{
return left;
}
else{
return NULL;
}
}
};
感觉理解上还是稍微有些问题(给上流程图)