Leetcode530.二叉搜索树的最小绝对差
文章链接:代码随想录
题目链接:530.二叉搜索树的最小绝对差
思路:和 98.验证二叉搜索树 昨天这题一样,构建中序数组或者直接中序遍历验证。
构建中序数组:
/**
* 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 traversal(TreeNode* node, vector<int>& vec){
if (node == NULL) return;
traversal(node->left, vec);
vec.push_back(node->val);
traversal(node->right, vec);
}
int getMinimumDifference(TreeNode* root) {
vector<int> vec;
traversal(root, vec);
int min = INT_MAX;
for (int i = 1; i < vec.size(); i++){
min = min > (vec[i] - vec[i - 1]) ? (vec[i] - vec[i - 1]) : min;
}
return min;
}
};
直接递归:
class Solution {
public:
TreeNode* pre = NULL;
int min = INT_MAX;
int traversal(TreeNode* node){
if (node == NULL) return NULL;
traversal(node->left);
if (pre != NULL) min = min > (node->val - pre->val) ? (node->val - pre->val) : min;
pre = node;
traversal(node->right);
return min;
}
int getMinimumDifference(TreeNode* root) {
return traversal(root);
}
};
Leetcode501.二叉搜索树中的众数
文章链接:代码随想录
题目链接:501.二叉搜索树中的众数
思路:两种,一种是构建中序数组,另一种是直接中序递归。
比较的方式也有两种,一种是遍历两次数组或者遍历两次树,一次遍历出max_count,另一次遍历出 count == max_count 的元素/树的值。或者在全局定义出count和max_count 一次遍历得出结果。
这题注意看递归法把count的赋值和结果的存取分离开来的写法,写构建中序数组写法时,没分开,在这debug了好久,包括数组越界、死循环、未考虑边界值的bug和错误,很别扭,还是熟悉掌握递归法中的写法。
构建中序数组:
/**
* 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 traversal(TreeNode* node, vector<int>& vec){
if (node == NULL) return ;
traversal(node->left, vec);
vec.push_back(node->val);
traversal(node->right, vec);
}
vector<int> findMode(TreeNode* root) {
vector<int> vec;
vector<int> res;
traversal(root, vec);
/*
// 日志
cout << "vec" << endl;
for (int j : vec){
cout << j;
}
cout << endl;
*/
int max_count = 1;
int count = 1;
int i;
for (i = 1; i < vec.size(); i++){
if (vec[i - 1] == vec[i]) count++;
else{
if (count == max_count) res.push_back(vec[i - 1]);
if (count > max_count) {
max_count = count;
res.clear();
res.push_back(vec[i - 1]);
}
count = 1;
}
}
if (count == max_count) res.push_back(vec[i - 1]);
if (count > max_count) {
res.clear();
res.push_back(vec[i - 1]);
}
return res;
}
};
中序递归:
class Solution {
public:
int count = 0;
int max_count = 0;
TreeNode* pre = NULL;
void traversal(TreeNode* node, vector<int>& res){
if (node == NULL) return ;
traversal(node->left, res);
if (pre == NULL) count = 1;
else if (pre->val == node->val) count++;
else count = 1;
pre = node;
if (count == max_count) res.push_back(node->val);
if (count > max_count){
max_count = count;
res.clear();
res.push_back(node->val);
}
traversal(node->right, res);
}
vector<int> findMode(TreeNode* root) {
vector<int> res;
traversal(root, res);
return res;
}
};
Leetcode236. 二叉树的最近公共祖先
文章链接:代码随想录
题目链接:236. 二叉树的最近公共祖先
思路:后序遍历就是在一种回溯,重点在于返回什么值,这题返回找到的最近公共祖先节点,注意写好几种判断条件。
/**
* 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* l = lowestCommonAncestor(root->left, p, q);
TreeNode* r = lowestCommonAncestor(root->right, p, q);
if (l && r) return root;
else if (l && !r) return l;
else if (r && !l) return r;
return NULL;
}
};
第二十一天打卡,在找众数处理逻辑那改了好久,还是掌握把记录count和存取结果分开的写法,另外可以用两次遍历来写,过几天有时间写一下。
加油!!!