一、修剪二叉搜索树
1.题目:给你二叉搜索树的根节点 root
,同时给定最小边界low
和最大边界 high
。通过修剪二叉搜索树,使得所有节点的值在[low, high]
中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
2.思路:
3.注意:
4.代码:
// /**
// * 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* leftNode;
TreeNode* rightNode;
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == NULL)
return NULL;
if( root->val < low )
{
leftNode = trimBST(root->right, low, high);
return leftNode;
}
if( root->val > high)
{
rightNode = trimBST(root->left, low, high);
return rightNode;
}
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
// class Solution {
// public:
// TreeNode* trimBST(TreeNode* root, int low, int high) {
// if (root == nullptr ) return nullptr;
// if (root->val < low) {
// TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点
// return right;
// }
// if (root->val > high) {
// TreeNode* left = trimBST(root->left, low, high); // 寻找符合区间[low, high]的节点
// return left;
// }
// root->left = trimBST(root->left, low, high); // root->left接入符合条件的左孩子
// root->right = trimBST(root->right, low, high); // root->right接入符合条件的右孩子
// return root;
// }
// };
二、将有序数组转换成二叉搜索树
1.题目:给你一个整数数组 nums
,其中元素已经按 升序 排列,请你将其转换为一棵
平衡二叉搜索树。
2.思路:
3.注意:
4.代码:
/**
* 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* traversal(vector<int>& nums, int leftIndex, int rightIndex)
{
if( leftIndex > rightIndex)
return NULL;
int cenIndex = leftIndex + (rightIndex - leftIndex)/2;
TreeNode* root = new TreeNode(nums[cenIndex]);
root->left = traversal(nums, leftIndex, cenIndex - 1);
root->right = traversal(nums, cenIndex + 1, rightIndex);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return traversal(nums, 0, nums.size()-1);
}
};
三、把二叉搜索树转换为累加树
1.题目:给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node
的新值等于原树中大于或等于 node.val
的值之和。
提醒一下,二叉搜索树满足下列约束条件:
- 节点的左子树仅包含键 小于 节点键的节点。
- 节点的右子树仅包含键 大于 节点键的节点。
- 左右子树也必须是二叉搜索树。
2.思路:反中序迭代:右→中→左
3.注意:
4.代码:
/**
* 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;
TreeNode* convertBST(TreeNode* root) {
if(root == NULL)
return NULL;
convertBST(root->right);
count += root->val;
root->val = count;
convertBST(root->left);
return root;
}
};