topic:
669. Pruning a Binary Search Tree
Given the root node of your binary search tree root
, you are given a minimum bound low
and a maximum bound high
. By pruning the binary search tree, the values of all nodes are in [low, high]
. Pruning the tree should not change the relative structure of the elements that remain in the tree (ie, the original parent-child relationship should be preserved if not removed). It can be shown that there is a unique answer .
So the result should return the new root node of the pruned binary search tree. Note that the root node may change according to the given bounds.
Example 1:
Input: root = [1,0,2], low = 1, high = 2 Output: [1,null,2]
Example 2:
Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3 Output: [3,2,null,1]
hint:
- The number of nodes in the tree is
[1, 104]
within the range 0 <= Node.val <= 104
- The value of each node in the tree is unique
- The title data guarantees that the input is a valid binary search tree
0 <= low <= high <= 104
answer:
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;
}
};
topic:
108. Convert Sorted Array to Binary Search Tree
Given an array of integers nums
in which the elements are sorted in ascending order, please convert it into a height-balanced binary search tree.
A height-balanced binary tree is a binary tree that satisfies "the absolute value of the height difference between the left and right subtrees of each node does not exceed 1".
Example 1:
Input: nums = [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Explanation: [0,-10,5,null,-3,null ,9] will also be considered the correct answer:
Example 2:
Input: nums = [1,3] Output: [3,1] Explanation: [1,null,3] and [3,1] are height balanced binary search trees.
hint:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums
in strict ascending order
Thinking process and knowledge points:
The essence is to find the split point, the split point is the current node, and then recurse the left and right intervals .
The split point is the node in the middle of the array.
So here comes the question, if the length of the array is even and there are two intermediate nodes, which one to choose?
Either one can be chosen, but a different balanced binary search tree is formed.
answer:
class Solution {
private:
TreeNode* traversal(vector<int>& nums, int left, int right) {
if (left > right) return nullptr;
int mid = left + ((right - left) / 2);
TreeNode* root = new TreeNode(nums[mid]);
root->left = traversal(nums, left, mid - 1);
root->right = traversal(nums, mid + 1, right);
return root;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root = traversal(nums, 0, nums.size() - 1);
return root;
}
};
topic:
538. Convert Binary Search Tree to Accumulating Tree
Given the root node of a binary search tree, the tree has different node values, please convert it into a cumulative tree (Greater Sum Tree), so that the node
new value of each node is equal to the value greater than or equal to node.val
the value in the original tree and.
As a reminder, a binary search tree satisfies the following constraints:
- A node's left subtree contains only nodes whose keys are less than the node's key.
- A node's right subtree contains only nodes whose keys are greater than the node's key.
- The left and right subtrees must also be binary search trees.
Note: This question is the same as 1038: Lituo
Example 1:
输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input: root = [0,null,1] Output: [1,null,1]
Example 3:
Input: root = [1,0,2] Output: [3,3,2]
Example 4:
Input: root = [3,2,4,1] Output: [7,9,4,10]
hint:
- The number of nodes in the tree is between
0
and104
. - Each node has a value between
-104
and104
. - All values in the tree are distinct from each other .
- The given tree is a binary search tree.
answer:
class Solution {
private:
int pre = 0; // 记录前一个节点的数值
void traversal(TreeNode* cur) { // 右中左遍历
if (cur == NULL) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->left);
}
public:
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};
Welcome to like, bookmark, comment, your encouragement is the biggest motivation for my creation! (๑╹◡╹)ノ"""
Copyright statement: This article is an original article of CSDN blogger "Dumengjiu", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: Dumengjiu's blog_CSDN blog-csdn domain blogger