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:
vector<int> path;
void getInPath(TreeNode* cur) {
if (cur == nullptr)
return;
getInPath(cur->left);
path.push_back(cur->val);
getInPath(cur->right);
}
int getMinimumDifference(TreeNode* root) {
getInPath(root);
int minn = INT_MAX;
for (int i = 1; i < path.size(); i++) {
minn = min(minn, path[i] - path[i - 1]);
}
return minn;
}
};
递归(记录上一个的指针)
/**
* 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:
TreeNode* pre = nullptr;
int res = INT_MAX;
void search(TreeNode* cur) {
if (cur == nullptr)
return;
search(cur->left);
if (pre != nullptr) {
res = min(res, cur->val - pre->val);
}
pre = cur;
search(cur->right);
}
int getMinimumDifference(TreeNode* root) {
search(root);
return res;
}
};
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 {
public:
vector<int> path;
void search(TreeNode* cur) {
if (cur == nullptr)
return;
search(cur->left);
path.push_back(cur->val);
search(cur->right);
}
vector<int> findMode(TreeNode* root) {
search(root);
int count = 0;
int maxcount = 0;
int i, j;
vector<int> res;
for (i = 0; i < path.size();) {
j = i;
while (j < path.size() && path[j] == path[i])
j++;
count = j - i;
if (count == maxcount)
res.push_back(path[i]);
if (count > maxcount) {
res.clear();
res.push_back(path[i]);
maxcount = count;
}
i = j;
}
return res;
}
};
下面的代码适合于任何二叉树
/**
* 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:
unordered_map<int, int> mp;
void search(TreeNode* cur) {
if (cur == nullptr)
return;
search(cur->left);
mp[cur->val]++;
search(cur->right);
}
vector<int> findMode(TreeNode* root) {
search(root);
vector<pair<int, int>> r(mp.begin(), mp.end());
sort(r.begin(), r.end(),
[](const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
});
vector<int> res;
res.push_back(r[0].first);
for (int i = 1; i < r.size(); i++) {
if (r[i].second == r[0].second)
res.push_back(r[i].first);
}
return res;
}
};
遍历肯定是:中序遍历,这样遍历出的节点是递增的
使用一个指向上一个节点的指针pre,将当前节点和上一个节点进行比较来进行相同值的记数
如果当前数的数量等于最大数量,则将其放入结果数组
当然之后该数的数量可能还会增加,因为还没有遍历完,所以我们再判断,如果当前数的数量大于最大数量,则将结果数组清空,将该数放入结果数组
/**
* 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 count = 0;
int maxcount = 0;
TreeNode* pre = nullptr;
vector<int> res;
void search(TreeNode* cur) {
if (cur == nullptr)
return;
search(cur->left);
if (pre == nullptr) {
count = 1;
} else if (pre->val == cur->val) {
count++;
} else {
count = 1;
}
if (count == maxcount) {
res.push_back(cur->val);
}
if (count > maxcount) {
maxcount = count;
res.clear();
res.push_back(cur->val);
}
pre = cur;
search(cur->right);
return;
}
vector<int> findMode(TreeNode* root) {
search(root);
return res;
}
};
236. 二叉树的最近公共祖先(重要)
第一种情况:某个非p非q的节点是他们俩的公共祖先,只需要寻找左子树和右子树里是否存在p和q,如果有则返回即可。
第二种情况:p是q的祖先或q是p的祖先,这种情况再代码中可以一起和第一种情况实现。
/**
* 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* left = lowestCommonAncestor(root->left,p,q);
TreeNode* right = lowestCommonAncestor(root->right,p,q);
if(left == NULL && right == NULL) return NULL;
// 返回的是p或q的某个祖先,但不是公共祖先,在某层递归里,如果左右递归都有返回值,那么返回的是root:即公共祖先
// 可以用返回left/right传给上一层结果,告诉上层函数已经找到了某个节点。
else if(left != NULL && right ==NULL) return left;
else if(left == NULL && right != NULL) return right;
// 返回公共祖先
else return root;
}
};