530.二叉搜索树的最小绝对差
双指针,创建一个新节点pre用于存放前一个节点,便于在遍历的过程中进行比较
仍是中序遍历,需要改变的就是中间的过程。
/**
* 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 {
private:
int ans = INT_MAX;
TreeNode* pre = NULL;
public:
void traversal(TreeNode* cur){
if(cur==NULL) return;
//中序
traversal(cur->left);//左
if(pre!=NULL) ans = min(ans, cur->val - pre->val); //中
pre = cur;
traversal(cur->right); //右
return;
}
int getMinimumDifference(TreeNode* root) {
traversal(root);
return ans;
}
};
501.二叉搜索树中的众数
501. 二叉搜索树中的众数
1.非二叉搜索树的方法
通过遍历统计每个数出现的频率,然后在对频率进行排序,将最高的取出。
使用map记录频率,由于map无法对value排序,需要额外建立vector来对频率排序
/**
* 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:
//非二叉搜索树
void search(TreeNode* root, unordered_map<int, int>& mymap){
if(root==NULL) return;
mymap[root->val]++;
search(root->left, mymap);
search(root->right, mymap);
}
//降序
bool static cmp(const pair<int, int>& pair1, const pair<int, int>& pair2){
return pair1.second > pair2.second;
}
vector<int> findMode(TreeNode* root) {
vector<int> ans;
unordered_map<int, int> mymap;
search(root, mymap);
//map无法对value排序,需要转化为vector
vector<pair<int, int>> vec(mymap.begin(), mymap.end());
sort(vec.begin(), vec.end(), cmp);
ans.push_back(vec[0].first);
for(int i=1; i<vec.size(); i++){
if(vec[0].second == vec[i].second) ans.push_back(vec[i].first);
else break;
}
return ans;
}
};
2.双指针法
/**
* 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 count = 0;
int maxcount = 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(maxcount == count){
result.push_back(cur->val);
}
if(maxcount < count){
maxcount = count;
result.clear();
result.push_back(cur->val);
}
searchBST(cur->right); //右
return;
}
vector<int> findMode(TreeNode* root) {
searchBST(root);
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) return root;
if(root==p || root==q) return root;
TreeNode* findleft = lowestCommonAncestor(root->left, p ,q);
TreeNode* findright = lowestCommonAncestor(root->right, p ,q);
if(findleft!=NULL && findright!=NULL){
return root;
}
else if(findleft==NULL){
return findright;
}
return findleft;
}
};