669. 修剪二叉搜尋樹
題目連結:https://leetcode.cn/problems/trim-a-binary-search-tree/
題目難度:中等
文章講解:https://programmercarl.com/0669.修剪二叉搜尋樹.html
影片講解:https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web
題目狀態:沒有思路,看題解過
思路:
使用遞迴。
- 引數和返回值:引數是節點,和左右邊界,返回值是節點。
- 終止條件:當節點為
nullptr
時,終止。 - 單層迴圈:噹噹前節點的值小於最小邊界時,將其右孩子加入二叉樹中進行遍歷;噹噹前節點的值大於最大邊界時,將其左孩子加入二叉樹中進行遍歷。
程式碼實現:
/**
* 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* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
if(root->val < low) {
TreeNode *right = trimBST(root->right, low, high);
return right;
}
if(root->val > high) {
TreeNode *left = trimBST(root->left, low, high);
return left;
}
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
迭代法程式碼:
/**
* 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* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
while(root != nullptr && (root->val < low || root->val > high)) {
if(root->val < low) root = root->right;
else root = root->left;
}
TreeNode *cur = root;
while(cur != nullptr) {
while(cur->left && cur->left->val < low) cur->left = cur->left->right;
cur = cur->left;
}
cur = root;
while(cur != nullptr) {
while(cur->right && cur->right->val > high) cur->right = cur->right->left;
cur = cur->right;
}
return root;
}
};
108. 將有序陣列轉換為二叉搜尋樹
題目連結:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
題目難度:簡單
文章講解:https://programmercarl.com/0108.將有序陣列轉換為二叉搜尋樹.html
影片講解:https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web
題目狀態:依舊是看題解
思路:
以陣列的中間為根結點,分別擷取陣列的左半部分作為根結點的左孩子,擷取陣列的右半部分作為根結點的右孩子。
程式碼:
/**
* 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 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;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode *root = traversal(nums, 0, nums.size() - 1);
return root;
}
};
538. 把二叉搜尋樹轉換為累加樹
題目連結:https://leetcode.cn/problems/convert-bst-to-greater-tree/
題目難度:中等
文章講解:https://programmercarl.com/0538.把二叉搜尋樹轉換為累加樹.html
影片講解:https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web
題目狀態:依舊是看題解
思路:
透過一個int
型別的全域性變數來儲存比當前節點大的節點的值,並使用右中左
的順序來遍歷二叉樹。
程式碼:
/**
* 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 pre = 0;
void traversal(TreeNode* cur) {
if(cur == nullptr) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->left);
}
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};
迭代法:
/**
* 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 pre;
void traversal(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
while(cur != nullptr || !st.empty()) {
if(cur != nullptr) {
st.push(cur);
cur = cur->right;
} else {
cur = st.top();
st.pop();
cur->val += pre;
pre = cur->val;
cur = cur->left;
}
}
}
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};