二叉搜索树是有序的!!!
530.二叉搜索树的最小绝对差
解法一:将二叉搜索树转化为有序数组,求数组中的最大差值。
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();
int result = INT_MAX;
traversal(root);
if(vec.size() < 2)return 0;
for(int i = 1; i < vec.size(); i++){
result = min(result, vec[i] - vec[i-1]);
}
return result;
}
};
解法二:利用递归,中序遍历,双指针法,比较每两个节点的差值,找到最小差值。
class Solution {
public:
int result = INT_MAX;
TreeNode* pre = NULL;
void traversal(TreeNode* cur){
if(cur == NULL)return;
traversal(cur->left);
if(pre != NULL){
result = min(result, cur->val - pre->val) ;
}
pre = cur;
traversal(cur->right);
}
int getMinimumDifference(TreeNode* root) {
traversal(root);
return result;
}
};
解法三:利用迭代法
class Solution {
public:
//迭代法
int getMinimumDifference(TreeNode* root) {
if(root == NULL)return 0;
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;
}
};
501.二叉搜索树中的众数
解法一:双指针递归
class Solution {
private:
TreeNode* pre = NULL;
int count = 0; //统计每一个元素出现的频次
int maxCount = 0; //众数
vector<int>result; //该值出现频次是众数,将该节点的值放入结果中
void traversal(TreeNode* cur){
if(cur == NULL)return;
traversal(cur->left);
if(pre == NULL)count = 1;//cur指向最左边的叶子结点,此时才开始从下往上遍历
else if(pre->val == cur->val)count++; //cur与指向前一个节点的指针指向的值相等,那么count++
//pre与cur指向的节点值不相等了,意味着有一个新的值要重新计数
else count = 1;
//比较count与maxcoun
if(count == maxCount){
result.push_back(cur->val);
}
//出现比现有最高频次还高的值了,将原来的一切都删除
if(count > maxCount){
result.clear();
maxCount = count;
result.push_back(cur->val);
}
pre = cur; //更新上一个节点
traversal(cur->right);
return;
}
public:
vector<int> findMode(TreeNode* root) {
count = 0;
maxCount = 0;
TreeNode* pre = NULL;
result.clear();
traversal(root);
return result;
}
};
解法二:迭代法
class Solution {
public:
TreeNode* pre = NULL;
int count = 0;
int maxCount = 0;
vector<int>result;
stack<TreeNode*>st;
vector<int> findMode(TreeNode* cur) {
if(cur == NULL) return 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;
pre = cur;//更新前一个节点
if(count == maxCount){
result.push_back(cur->val);
}
else if(count > maxCount){
result.clear();
maxCount = count;
result.push_back(cur->val);
}
cur = cur->right;
}
}
return result;
}
};
236. 二叉树的最近公共祖先
从底向上遍历,找到q,p的最近公共祖先。分为以下几种情况:
(1)p,q分别在一个二叉树的左右子树中;
(2)p,q在一个二叉树的其中一个子树上;
(3)在二叉树 的左右子树都无法找到,返回NULL;
(4)当前p或q节点同时是最近公共祖先。
class Solution {
public:
//采用后序遍历
TreeNode* traversal(TreeNode* root, TreeNode* p, TreeNode* q){
if(root == NULL)return NULL;
if(root == p || root == q)return root;
TreeNode* left = traversal(root->left, p, q); //在左子树找是否有节点等于p或q
TreeNode* right = traversal(root->right, p, q);//在右子树找是否有节点等于p或q
if(left != NULL && right != NULL)return root;
else if(left == NULL && right != NULL)return right;//左子树为空(就是没找到),返回右子树
else if(left != NULL && right == NULL)return left;
else return NULL;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return traversal(root, p, q);
}
};