0,定义
二叉搜索树(Binary Search Tree),又名二叉查找树,二叉排序树,它或者是一棵空树,或者具有下列性质:
- 左子树上所有结点的值均小于根节点的值;
- 右子树上所有结点的值均大于根结点的值;
- 左、右子树也分别为二叉搜索树;
二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势,所以应用十分广泛。
根据二叉搜索树的性质,我们得出二叉搜索树的中序遍历结果将是有序的。
1,验证二叉搜索树
来源:98. 验证二叉搜索树 && 面试题 04.05. 合法二叉搜索树
中序遍历二叉搜索树即可,注意节点值越界。
class Solution {
public:
long pre;
bool isValidBST(TreeNode* root) {
pre = -9999999999;
return visit(root);
}
bool visit(TreeNode* root) {
if (root == NULL) return true;
if (!visit(root->left)) return false;
if (root->val <= pre) return false;
pre = root->val;
if (!visit(root->right)) return false;
return true;
}
};
2,二叉搜索树节点最小差值
来源:530. 二叉搜索树的最小绝对差 && 783. 二叉搜索树节点最小距离
计算树中任意两节点的差的绝对值的最小值,首先中序遍历二叉搜索树并存储结果,计算相邻2个数字的差,返回最小差。代码请忽略空间复杂度。
class Solution {
public:
queue<int> q;
int getMinimumDifference(TreeNode* root) {
visit(root);
int min = -1;
int last = q.front(); q.pop();
while (!q.empty()) {
int curr = q.front(); q.pop();
int t = curr - last;
last = curr;
if (min < 0 || min > t) min = t;
}
return min;
}
void visit(TreeNode* root) {
if (root == NULL) return;
visit(root->left);
q.push(root->val);
visit(root->right);
}
};
3,给定范围内所有节点的和
当前节点与给定范围比较,不在范围内则递归判断左右子树其中一支,否则当前+左右子树结果并返回。
class Solution {
public:
int low_;
int high_;
int rangeSumBST(TreeNode* root, int low, int high) {
low_ = low;
high_ = high;
return visit(root);
}
int visit(TreeNode* root) {
if (root == NULL) return 0;
if (root->val < low_) {
return visit(root->right);
} else if (root->val > high_) {
return visit(root->left);
} else {
return root->val + visit(root->right) + visit(root->left);
}
}
};
4,搜索二叉搜索树
类似二分查找。
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL || root->val == val) return root;
if (root->val < val) return searchBST(root->right, val);
else return searchBST(root->left, val);
}
};
5,第k大(小)的数
来源:230. 二叉搜索树中第K小的元素 && 剑指 Offer 54. 二叉搜索树的第k大节点
中序遍历,记录遍历的个数。第k大则先遍历右子树,第k小则先遍历左子树。
class Solution {
public:
int result;
int kk;
int kthSmallest(TreeNode* root, int k) {
result = 0;
kk = k;
visit(root);
return result;
}
// 第k小:左中右,第k大:右中左
void visit(TreeNode* root) {
if (root == NULL || kk <= 0) return;
visit(root->left); // 左
if (--kk == 0) {
result = root->val;
return;
}
visit(root->right); // 右
}
};
6,最近公共祖先
来源:剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 && 235. 二叉搜索树的最近公共祖先
元素节点不重复的情况下,找到2个指定节点的最近公共祖先。
二叉树的最近公共祖先,未利用二叉搜索树的性质,代码如下:
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if (root == NULL) return NULL;
if (root->val == p->val || root->val == q->val) return root;
struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
struct TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left != NULL && right != NULL) return root;
return left == NULL ? right : left;
}
利用性质后的代码,与题4搜索类似。
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL) return NULL;
if (root->val == p->val || root->val == q->val) return root;
if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
return root;
}
};
7,出现频次最多的数字列表
中序遍历过程中累加每个数字的频次,输出一个数组:[数字,频次,数字,频次,,,,],计算最大频次max,将符合max的所有数字输出。
class Solution {
public:
int pre;
int freq;
vector<int> value_freq;
vector<int> findMode(TreeNode* root) {
vector<int> result;
if (root == NULL) return result;
pre = root->val;
freq = 0;
visit(root);
if (freq > 0) {
value_freq.push_back(pre);
value_freq.push_back(freq);
}
int max = 0;
for (int i = 1; i < value_freq.size(); i+=2) {
if (value_freq[i] > max) max = value_freq[i];
}
for (int i = 1; i < value_freq.size(); i+=2) {
if (value_freq[i] == max) result.push_back(value_freq[i-1]);
}
return result;
}
void visit(TreeNode* root) {
if (root->left) visit(root->left);
if (root->val == pre) {
freq++;
} else {
value_freq.push_back(pre);
value_freq.push_back(freq);
pre = root->val;
freq = 1;
}
if (root->right) visit(root->right);
}
};
8,二叉搜索树迭代器
提前中序遍历树,存储结果。
class BSTIterator {
public:
list<int> data;
BSTIterator(TreeNode* root) {
go(root);
}
void go(TreeNode* root) {
if (root == NULL) return;
go(root->left);
data.push_back(root->val);
go(root->right);
}
int next() {
int v = data.front();
data.pop_front();
return v;
}
bool hasNext() {
return !data.empty();
}
};
可以使用栈,一直入栈左节点直到左节点不存在,出栈后将右子树的左节点一直入栈。
class BSTIterator {
public:
stack<TreeNode*> data;
BSTIterator(TreeNode* root) {
while (root) {
data.push(root);
if (root->left) root = root->left;
else break;
}
}
int next() {
TreeNode* p = data.top(); data.pop();
int ret = p->val;
p = p->right;
while (p) {
data.push(p);
if (p->left) p = p->left;
else break;
}
return ret;
}
bool hasNext() {
return !data.empty();
}
};
前者空间O(N),后者空间O(H),H为树高。二者时间复杂度O(1)。
9,转换为累加树
来源:538. 把二叉搜索树转换为累加树 && 1038. 把二叉搜索树转换为累加树
把二叉搜索树想象成一个有序的数组,从后向前开始累加,所以遍历顺序:右中左。
class Solution {
public:
int sum;
TreeNode* convertBST(TreeNode* root) {
sum = 0;
visit(root);
return root;
}
void visit(TreeNode* root) {
if (root == NULL) return;
visit(root->right);
root->val += sum;
sum = root->val;
visit(root->left);
}
};
10,有序输出2课二叉搜索树的所有元素
先中序遍历2棵树,生成2个有序的元素序列,归并输出。
class Solution {
public:
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> a;
visit(root1, &a);
vector<int> b;
visit(root2, &b);
vector<int> result;
merge(a, b, &result);
return result;
}
void visit(TreeNode *root, vector<int> *ret) {
if (root == NULL) return;
visit(root->left, ret);
ret->push_back(root->val);
visit(root->right, ret);
}
void merge(const vector<int>& a, const vector<int>& b, vector<int> *ret) {
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] < b[j]) ret->push_back(a[i++]);
else ret->push_back(b[j++]);
}
if (i == a.size()) {
ret->insert(ret->end(), b.begin() + j, b.end());
} else {
ret->insert(ret->end(), a.begin() + i, a.end());
}
}
};