LeetCode530.二叉搜索树的最小绝对差
递归法双指针实现二叉树的中序遍历,代码如下:
class Solution {
public:
int maxValue = INT_MAX;
TreeNode* pre = nullptr;
int getMinimumDifference(TreeNode* root) {
if(root==nullptr) return 0;
getMinimumDifference(root->left);
if(pre && root->val-pre->val<maxValue){
maxValue = root->val-pre->val;
}
pre = root;
getMinimumDifference(root->right);
return maxValue;
}
};
也可以使用迭代的思路,卡哥代码如下:
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;
}
};
LeetCode501.二叉搜索树中的众数
最直接的方法,首先遍历二叉树,用哈希表维护每个元素的频率,然后再遍历一遍,找出最大频率的元素,这个思路卡哥代码如下:
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;
}
};
那么其实可以只用单次遍历就可以直接求得最大的频率和对应的数值,利用二叉搜索树的特点,使用双指针,维护两个成员变量maxCount和count,代码如下:
一开始写这个代码的时候将count与maxCount的判断逻辑放到了值判断是否的相等的模块中,导致无法更新叶子节点的频率,如果要对当下cur节点进行处理,需要在每一次递归时都要判断一遍。
class Solution {
public:
TreeNode* pre = nullptr;
int maxCount = 0;
int count = 0;
vector<int> res;
void traversal(TreeNode* node){
if(node==nullptr) return;
traversal(node->left);
if(!pre) count=1;
else if(node->val==pre->val){
count++;
}else if(node->val!=pre->val){
count=1;
}
if(count>maxCount){
maxCount = count;
res.clear();
res.push_back(node->val);
}else if(count==maxCount){
res.push_back(node->val);
}
pre = node;
traversal(node->right);
}
vector<int> findMode(TreeNode* root) {
traversal(root);
return res;
}
};
双指针迭代法逻辑,卡哥代码如下:
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;
}
};
LeetCode236. 二叉树的最近公共祖先
这题虽然代码不长,但确实有难度,思路也确实很巧妙。
审题:一定包含p和q,并且每个节点的val互不相同
递归逻辑:如果左子树包含p或q,右子树包含p或q,则返回当前节点,当前节点即为最近公共祖先,后面需要做的就是不停将此节点向上返回。
这里面也包含了公共祖先就是p或q节点的情况。
代码如下:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==nullptr) return nullptr;
if(root==p || root==q) return root;
TreeNode* left = lowestCommonAncestor(root->left,p,q);
TreeNode* right = lowestCommonAncestor(root->right,p,q);
if(left && right) return root;
if(!left && right) return right;
if(left && !right) return left;
return nullptr;
}
};