代码随想录: 代码随想录
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 {
private:
unordered_map<int,int> mp;
void traversal(TreeNode* root) {
queue<TreeNode*> q;
if (root != nullptr) q.push(root);
while (!q.empty()) {
int size = q.size();
while (size--) {
TreeNode* node = q.front();
mp[node->val]++;
if (node->left != nullptr) q.push(node->left);
if (node->right != nullptr) q.push(node->right);
q.pop();
}
}
}
public:
vector<int> findMode(TreeNode* root) {
traversal(root);
vector<int> ans;
int maxCount = 0;
for (auto p : mp) {
maxCount = max(maxCount, p.second);
}
for (auto p : mp) {
if (p.second == maxCount) {
ans.push_back(p.first);
}
}
return ans;
}
};
二叉搜索树的方法:
501. 二叉搜索树中的众数
一般方法:
/**
* 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 {
private:
void traversal(TreeNode* root, unordered_map<int,int>& mp) {
queue<TreeNode*> q;
if (root != nullptr) q.push(root);
while (!q.empty()) {
int size = q.size();
while (size--) {
TreeNode* node = q.front();
mp[node->val]++;
if (node->left != nullptr) q.push(node->left);
if (node->right != nullptr) q.push(node->right);
q.pop();
}
}
}
public:
vector<int> findMode(TreeNode* root) {
unordered_map<int,int> mp;
traversal(root, mp);
vector<int> ans;
int maxCount = 0;
for (auto p : mp) {
maxCount = max(maxCount, p.second);
}
for (auto p : mp) {
if (p.second == maxCount) {
ans.push_back(p.first);
}
}
return ans;
}
};
双指针:
/**
* 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 {
private:
int curCount = 0, maxCount = 0;
TreeNode* pre = nullptr;
vector<int> res;
void traversal(TreeNode* cur) {
if (cur == nullptr)
return;
traversal(cur->left);
if (pre == nullptr)
curCount = 1;
else if (pre->val == cur->val) {
curCount++;
} else {
curCount = 1;
}
pre = cur;
if (curCount == maxCount) {
res.push_back(cur->val);
} else if (curCount > maxCount) {
maxCount = curCount;
res.clear();
res.push_back(cur->val);
}
traversal(cur->right);
}
public:
vector<int> findMode(TreeNode* root) {
traversal(root);
return res;
}
};
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 == NULL) return NULL;
if (root == p || root == q) 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;
if (left != NULL && right == NULL) return left;
return NULL;
}
};