LeetCode530.二叉搜索树的最小绝对差
题目链接:
思路:
用双指针,前指针记录前一个节点的值,后指针记录当前节点的值
/**
* 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 result = INT_MAX;
TreeNode* pre = NULL;
void travel(TreeNode* root) {
if(root == NULL) return;
travel(root->left);//左
if(pre != NULL) {//中
result = min(result, root->val - pre->val);
}
pre = root;
travel(root->right);//右
}
int getMinimumDifference(TreeNode* root) {
travel(root);
return result;
}
};
LeetCode501.二叉搜索树的众数
题目链接:501. 二叉搜索树中的众数 - 力扣(LeetCode)
思路:
1.不是二叉搜索树:最直观的方法一定是把这个树都遍历了,用map统计频率,把频率排个序,最后取前面高频的元素的集合
/**
* 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>& map) {
if(root == NULL) return;
map[root->val]++;//统计元素频率
search(root->left,map);
search(root->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;//key:元素,value:频率
vector<int> result;
if(root == NULL) return result;
search(root,map);
vector<pair<int,int>> vec(map.begin(),map.end());
sort(vec.begin(),vec.end(),cmp);//给频率排序
for(int i = 0;i < vec.size();i++) {
//取最高的放到result数组中
if(vec[i].second == vec[0].second) result.push_back(vec[i].first);
else break;
}
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 {
public:
int maxcount = 0;//最大频率
int count = 0;//统计频率
TreeNode* pre = NULL;
vector<int> result;
void search(TreeNode* cur) {
if(cur == NULL) return ;
search(cur->left);//左
//中
if(pre == NULL) count = 1;//第一个节点
else if(pre->val == cur->val) count++;//与前一个节点数值相同
else count = 1;//与前面一个节点数值不相同
pre = cur;//更新上一个节点
if(count == maxcount) {//如果和最大值相同,放进result
result.push_back(cur->val);
}
if(count > maxcount) {//如果计数大于最大值频率
maxcount = count;//更新最大频率
result.clear();//数组清空,之前元素作废
result.push_back(cur->val);//现在才是众数
}
search(cur->right);//右
return ;
}
vector<int> findMode(TreeNode* root) {
search(root);
return result;
}
};
LeetCode236.二叉树的最近公共祖先
题目链接:236. 二叉树的最近公共祖先 - 力扣(LeetCode)
思路:
最近公共祖先自底向上查,二叉树回溯的过程就是从低到上。
后序遍历(左右中)就是天然的回溯过程,可以根据左右子树的返回值,来处理中节点的逻辑。
1.如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。
2.节点本身p(q),它拥有一个子孙节点q(p)。
在递归函数有返回值的情况下:如果要搜索一条边,递归函数返回值不为空的时候,立刻返回,如果搜索整个树,直接用一个变量left、right接住返回值,这个left、right后序还有逻辑处理的需要,也就是后序遍历中处理中间节点的逻辑(也是回溯)。
/**
* 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 left;
else if(left == NULL && right != NULL) return right;
else return NULL;
}
};