day21
代码随想录
2023.12.19
1. 530二叉搜索树的最小绝对差
害,开始题目看错了,以为求的是相连节点,也就是父子节点最小绝对差,结果提交某些测试用例没通过,才发现求的是任意不同节点,这里把我写的父子节点最小绝对差的代码也展示下(写都写了,不能白写。。。)
/**
* 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;
travel(root, result);
return result;
}
int number(int a){
if(a>0) return a;
if(a<0) return -a;
return a;
}
void travel(TreeNode*node, int& result){
int min_left = 0;
int min_right = 0;
if(node->left==NULL && node->right==NULL)
return;
if(node->left!=NULL){
min_left=number(node->val - node->left->val);
result = (min_left < result) ? min_left:result;
travel(node->left,result);
}
if(node->right!=NULL ){
min_right=number(node->val - node->right->val);
result = (min_right < result) ? min_right:result;
travel(node->right,result);
}
}
};
对于这道题,我选择,直接中序遍历得数组,然后找相邻差最小的就行!
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
vector<int> result_Num;
travel(root, result_Num);
int result = INT_MAX;
for(int i=0;i<result_Num.size()-1;i++){
int temp = result_Num[i+1] - result_Num[i];
result = (temp<result) ? temp:result;
}
return result;
}
void travel(TreeNode*node, vector<int>& vec){
if(node->left) travel(node->left, vec);
vec.push_back(node->val);
if(node->right) travel(node->right, vec);
}
};
2. 501二叉搜索树中的众数
这道题思路很多,肯定需要遍历的,可以在遍历时统计频率,但逻辑可能会比较乱,最好理解的还是先保存遍历结果(顺序无要求),之后遍历结果数组,这里是频率,我很自然想到了用map,因此遍历数组,频率保存在map中,再找到最大频率,最后保存最大频率对应的key,也就是我们节点的val即可!可能有些麻烦,但思路很简单。
/**
* 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:
vector<int> findMode(TreeNode* root) {
vector<int> Mod;
vector<int> result;
travel(root, Mod);
unordered_map<int, int> freqMap;
for(auto num:Mod)
freqMap[num]++;
int maxCount = 0;
for (const auto& pair : freqMap) {
maxCount = std::max(maxCount, pair.second);
}
// 找到所有出现次数最多的值
for (const auto& pair : freqMap) {
if (pair.second == maxCount) {
result.push_back(pair.first);
}
}
return result;
}
void travel(TreeNode*node, vector<int>& vec){
if(node->left) travel(node->left, vec);
vec.push_back(node->val);
if(node->right) travel(node->right, vec);
}
};
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 == 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 right;
else if (left != NULL && right == NULL) return left;
else { // (left == NULL && right == NULL)
return NULL;
}
}
};