0.引言
● 669. 修剪二叉搜索樹
● 108.將有序數(shù)組轉(zhuǎn)換為二叉搜索樹
● 538.把二叉搜索樹轉(zhuǎn)換為累加樹
669. 修剪二叉搜索樹
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Medium (67.89%) | 782 | - |
給你二叉搜索樹的根節(jié)點(diǎn) root
概耻,同時(shí)給定最小邊界low
和最大邊界 high
霜浴。通過(guò)修剪二叉搜索樹,使得所有節(jié)點(diǎn)的值在[low, high]
中尸折。修剪樹 不應(yīng)該 改變保留在樹中的元素的相對(duì)結(jié)構(gòu) (即职祷,如果沒(méi)有被移除书聚,原有的父代子代關(guān)系都應(yīng)當(dāng)保留)。 可以證明,存在 唯一的答案 稿存。
所以結(jié)果應(yīng)當(dāng)返回修剪好的二叉搜索樹的新的根節(jié)點(diǎn)。注意瞳秽,根節(jié)點(diǎn)可能會(huì)根據(jù)給定的邊界發(fā)生改變瓣履。
示例 1:
輸入:root = [1,0,2], low = 1, high = 2
輸出:[1,null,2]
示例 2:
輸入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
輸出:[3,2,null,1]
提示:
- 樹中節(jié)點(diǎn)數(shù)在范圍
[1, 10<sup>4</sup>]
內(nèi) 0 <= Node.val <= 10<sup>4</sup>
- 樹中每個(gè)節(jié)點(diǎn)的值都是 唯一 的
- 題目數(shù)據(jù)保證輸入是一棵有效的二叉搜索樹
0 <= low <= high <= 10<sup>4</sup>
遞歸法
/*
* @lc app=leetcode.cn id=669 lang=cpp
*
* [669] 修剪二叉搜索樹
*/
// @lc code=start
/**
* 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) {
return dfs(root, low, high);
}
private:
// 后序遍歷
TreeNode* dfs(TreeNode* node, int low, int high) {
if (node == nullptr) {
return nullptr;
}
if (node->val < low) {
return dfs(node->right, low, high);
}
if (node->val > high) {
return dfs(node->left, low, high);
}
node->left = dfs(node->left, low, high);
node->right = dfs(node->right, low, high);
return node;
}
};
// @lc code=end
108. 將有序數(shù)組轉(zhuǎn)換為二叉搜索樹
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (77.34%) | 1263 | - |
給你一個(gè)整數(shù)數(shù)組 nums
,其中元素已經(jīng)按 升序 排列练俐,請(qǐng)你將其轉(zhuǎn)換為一棵 高度平衡 二叉搜索樹袖迎。
**高度平衡 **二叉樹是一棵滿足「每個(gè)節(jié)點(diǎn)的左右兩個(gè)子樹的高度差的絕對(duì)值不超過(guò) 1 」的二叉樹。
示例 1:
輸入:nums = [-10,-3,0,5,9]
輸出:[0,-3,9,-10,null,5]
解釋:[0,-10,5,null,-3,null,9] 也將被視為正確答案:
示例 2:
輸入:nums = [1,3]
輸出:[3,1]
解釋:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索樹腺晾。
提示:
1 <= nums.length <= 10<sup>4</sup>
-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup>
-
nums
按 嚴(yán)格遞增 順序排列
遞歸法
/*
* @lc app=leetcode.cn id=108 lang=cpp
*
* [108] 將有序數(shù)組轉(zhuǎn)換為二叉搜索樹
*/
// @lc code=start
/**
* 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* sortedArrayToBST(vector<int>& nums) {
return dfs(nums);
}
private:
TreeNode* dfs(std::vector<int> nums) {
if (nums.empty()) {
return nullptr;
}
if (nums.size() == 1) return new TreeNode(nums[0]);
int mid = nums.size() / 2;
TreeNode* node = new TreeNode(nums[mid]);
std::vector<int> left(nums.begin(), nums.begin() + mid);
std::vector<int> right(nums.begin() + mid + 1, nums.end());
node->left = dfs(left);
node->right = dfs(right);
return node;
}
};
// @lc code=end
538.把二叉搜索樹轉(zhuǎn)換為累加樹
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Medium (76.01%) | 864 | - |
給出二叉** 搜索 **樹的根節(jié)點(diǎn)燕锥,該樹的節(jié)點(diǎn)值各不相同,請(qǐng)你將其轉(zhuǎn)換為累加樹(Greater Sum Tree)悯蝉,使每個(gè)節(jié)點(diǎn) node
的新值等于原樹中大于或等于 node.val
的值之和归形。
提醒一下,二叉搜索樹滿足下列約束條件:
- 節(jié)點(diǎn)的左子樹僅包含鍵** 小于 **節(jié)點(diǎn)鍵的節(jié)點(diǎn)鼻由。
- 節(jié)點(diǎn)的右子樹僅包含鍵** 大于** 節(jié)點(diǎn)鍵的節(jié)點(diǎn)暇榴。
- 左右子樹也必須是二叉搜索樹。
注意:本題和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ 相同
示例 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]
示例 2:
輸入:root = [0,null,1]
輸出:[1,null,1]
示例 3:
輸入:root = [1,0,2]
輸出:[3,3,2]
示例 4:
輸入:root = [3,2,4,1]
輸出:[7,9,4,10]
提示:
- 樹中的節(jié)點(diǎn)數(shù)介于
0
和10<sup>4</sup>
之間蕉世。 - 每個(gè)節(jié)點(diǎn)的值介于
-10<sup>4</sup>
和10<sup>4</sup>
之間蔼紧。 - 樹中的所有值 互不相同 。
- 給定的樹為二叉搜索樹狠轻。
遞歸法
之前還想復(fù)雜了:
/*
* @lc app=leetcode.cn id=538 lang=cpp
*
* [538] 把二叉搜索樹轉(zhuǎn)換為累加樹
*/
// @lc code=start
/**
* 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* convertBST(TreeNode* root) {
if (root == nullptr) return nullptr;
int cumulate_val = 0;
bool is_right_most = false;
dfs(root, cumulate_val, is_right_most);
return root;
}
private:
// 右中左的遍歷順序
void dfs(TreeNode* node, int& cumulate_val, bool& is_right_most) {
// 找到最右邊那個(gè)
if (node->left == nullptr && node->right == nullptr && !is_right_most) {
is_right_most = true;
cumulate_val = node->val;
return;
}
// 正常的終止條件
// if (node->left == nullptr && node->right == nullptr) {
// node->val += cumulate_val;
// cumulate_val = node->val;
// return;
// }
if(node==nullptr) return;
if (node->right) dfs(node->right, cumulate_val, is_right_most);
node->val += cumulate_val;
cumulate_val = node->val;
if (node->left) dfs(node->left, cumulate_val, is_right_most);
}
};
// @lc code=end
這種情況歉井,[2,1]這種case過(guò)不了,其實(shí)完全不用人為的去找哈误,由于遍歷順序決定哩至,天然的就會(huì)去找到起始的位置:
/*
* @lc app=leetcode.cn id=538 lang=cpp
*
* [538] 把二叉搜索樹轉(zhuǎn)換為累加樹
*/
// @lc code=start
/**
* 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* convertBST(TreeNode* root) {
if (root == nullptr) return nullptr;
int cumulate_val = 0;
bool is_right_most = false;
dfs(root, cumulate_val);
return root;
}
private:
// 右中左的遍歷順序
void dfs(TreeNode* node, int& cumulate_val) {
if(node==nullptr) return;
dfs(node->right, cumulate_val);
node->val += cumulate_val;
cumulate_val = node->val;
dfs(node->left, cumulate_val);
}
};
// @lc code=end