day21打卡
- 递归法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
int ans = INT_MAX;
TreeNode* pre = nullptr;
int getMinimumDifference(TreeNode* root) {
dfs(root);
return ans;
}
void dfs(TreeNode* root)
{
//递归出口
if(root == nullptr) return;
//中序遍历
//左
dfs(root->left);
//中
if(pre != nullptr)
{
ans = min(ans, root->val - pre->val);
}
//记录前一个节点
pre = root;
//右
dfs(root->right);
}
};
- 迭代法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
stack<TreeNode*> st;
int ans = INT_MAX;
TreeNode* cur = root;
TreeNode* pre = nullptr;
while(!st.empty() || cur != nullptr)
{
if(cur != nullptr)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
st.pop();
if(pre != nullptr)
{
ans = min(ans, cur->val - pre->val);
}
pre = cur;
cur = cur->right;
}
}
return ans;
}
};
哈希表+遍历二叉搜索树
class Solution {
public:
unordered_map<int, int> hash;
struct cmp
{
bool operator()(const pair<int, int>& l, const pair<int, int>& r)
{
return l.second > r.second;
}
} cmp;
vector<int> findMode(TreeNode* root) {
vector<int> ret;
if(root == nullptr) return ret;
dfs(root);
vector<pair<int, int>> tmp(hash.begin(), hash.end());
sort(tmp.begin(), tmp.end(), cmp);
for(int i = 0; i < tmp.size(); i++)
{
if(tmp[0].second == tmp[i].second)
{
ret.push_back(tmp[i].first);
}
else break;
}
return ret;
}
void dfs(TreeNode* root)
{
if(root == nullptr) return;
hash[root->val]++;
dfs(root->left);
dfs(root->right);
return;
}
};
- 递归法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
vector<int> ans;
TreeNode* pre = nullptr;
int count = 0, maxCount = 0;
vector<int> findMode(TreeNode* root) {
dfs(root);
return ans;
}
void dfs(TreeNode* root)
{
if(root == nullptr) return;
dfs(root->left);
if(pre == nullptr)
{
count = 1;
}
else if(pre->val == root->val)
{
count++;
}
else if(pre->val != root->val)
{
count = 1;
}
pre = root;
if(count == maxCount)
{
ans.push_back(root->val);
}
if(count > maxCount)
{
maxCount = count;
ans.clear();
ans.push_back(root->val);
}
dfs(root->right);
}
};
- 迭代法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
vector<int> findMode(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = nullptr;
int count = 0, maxCount = 0;
vector<int> ans;
while(!st.empty() || cur != nullptr)
{
if(cur != nullptr)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
st.pop();
if(pre == nullptr)
{
count = 1;
}
else if(pre->val == cur->val)
{
count++;
}
else if(pre->val != cur->val)
{
count = 1;
}
if(count == maxCount)
{
ans.push_back(cur->val);
}
if(count > maxCount)
{
maxCount = count;
ans.clear();
ans.push_back(cur->val);
}
pre = cur;
cur = cur->right;
}
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == nullptr || root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left == nullptr) return right;
if(right == nullptr) return left;
return root;
}
};