1.二叉搜索树的最小绝对差
题目链接/文章讲解:代码随想录
视频讲解:二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差_哔哩哔哩_bilibili
代码:递归法 双指针
/**
* 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:
TreeNode* pre = NULL;
int result = INT_MAX;
void traversal(TreeNode* node){
if(node == NULL) return;
// 左
traversal(node->left);
// 中
if(pre != NULL && node->val - pre->val < result){
result = node->val - pre->val;
}
pre = node;
// 右
traversal(node->right);
}
int getMinimumDifference(TreeNode* root) {
traversal(root);
return result;
}
};
note:INT_MAX写错了。。。。
代码:迭代法 双指针
/**
* 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) {
int result = INT_MAX;
stack<TreeNode*> st;
if(root != NULL) st.push(root);
TreeNode* pre = NULL;
while(!st.empty()){
TreeNode* node = st.top();
if(node != NULL){
st.pop();
// 右
if(node->right) st.push(node->right);
// 中
st.push(node);
st.push(NULL);
// 左
if(node->left) st.push(node->left);
}else{
st.pop();
node = st.top();
st.pop();
if(pre && node->val - pre->val < result){
result = node->val - pre->val;
}
pre = node;
}
}
return result;
}
};
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 {
private:
int maxCount = 0;
int count = 0;
vector<int> result;
TreeNode* pre = NULL;
void searchBST(TreeNode* node){
if(node == NULL) return;
// 左
searchBST(node->left);
// 中
if(pre == NULL) count = 1;
if(pre != NULL && node->val == pre->val){
count++;
}
if(pre != NULL && node->val != pre->val){
count = 1;
}
pre = node;
if(count == maxCount){
result.push_back(node->val);
}else if(count > maxCount){
maxCount = count;
result.clear();
result.push_back(node->val);
}
// 右
searchBST(node->right);
}
public:
vector<int> findMode(TreeNode* root) {
searchBST(root);
return result;
}
};
代码:迭代法 双指针
class Solution {
public:
vector<int> findMode(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* pre = NULL;
int count = 0;
int maxCount = 0;
if(root != NULL) st.push(root);
while(!st.empty()){
TreeNode* node = st.top();
if(node != NULL){
st.pop();
// 右
if(node->right) st.push(node->right);
// 中
st.push(node);
st.push(NULL);
// 左
if(node->left) st.push(node->left);
}else{
st.pop();
node = st.top();
st.pop();
if(pre == NULL){
count = 1;
}
if(pre && node->val == pre->val){
count++;
}
if(pre && node->val != pre->val){
count = 1;
}
pre = node;
if(count == maxCount){
result.push_back(node->val);
}else if(count > maxCount){
maxCount = count;
result.clear();
result.push_back(node->val);
}
}
}
return result;
}
};
3.二叉树的最近公共祖先
代码:递归法
/**
* 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 == p || root == q || root == NULL) return root;
// 左
TreeNode* left = lowestCommonAncestor(root->left,p,q);
// 右
TreeNode* right = lowestCommonAncestor(root->right,p,q);
// 中
if(left == NULL && right != NULL){
return right;
}
if(left != NULL && right == NULL){
return left;
}
if(left != NULL && right != NULL){
return root;
}
return NULL;
}
};
迭代法不适合回溯模拟的过程。