Java?C++?算法題解leetcode669修剪二叉搜索樹示例
更新時間:2022年09月14日 09:43:39 作者:AnjaVon
這篇文章主要為大家介紹了Java?C++?算法題解leetcode669修剪二叉搜索樹示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
題目要求


思路一:模擬迭代
- 依次判斷每個節(jié)點是否合法:
- 首先找出結(jié)果的根,若原根小了就拉右邊的過來,大了拉左邊的過來做新根;
- 然后分別判斷左右子樹的大小,由于二叉搜索樹的性質(zhì),子樹只需要判斷一邊就好:
- 左子樹判斷是否>low,合法就向左下走,不合法往右下;
- 右子樹判斷是否<high,合法就向右下走,不合法往左下。
Java
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
while (root != null && (root.val < low || root.val > high)) // 確定原根是否合法
root = root.val < low ? root.right : root.left;
TreeNode res = root;
while (root != null) {
while (root.left != null && root.left.val < low)
root.left = root.left.right;
root = root.left;
}
root = res;
while (root != null) {
while (root.right != null && root.right.val > high)
root.right = root.right.left;
root = root.right;
}
return res;
}
}
- 時間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1)
C++
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
while (root != nullptr && (root->val < low || root->val > high)) // 確定原根是否合法
root = root->val < low ? root->right : root->left;
TreeNode* res = root;
while (root != nullptr) {
while (root->left != nullptr && root->left->val < low)
root->left = root->left->right;
root = root->left;
}
root = res;
while (root != nullptr) {
while (root->right != nullptr && root->right->val > high)
root->right = root->right->left;
root = root->right;
}
return res;
}
};
- 時間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1)
思路二:遞歸
- 直接用當前函數(shù)遞歸修剪即可:
- 當前值小了放右下(大)的值進去,剪掉當前和左邊節(jié)點;
- 當前值大了放左下(小)的值進去,剪掉當前和右邊節(jié)點。
- 然后遞歸掉下面所有節(jié)點。
Java
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if (root == null)
return null;
if (root.val < low)
return trimBST(root.right, low, high);
else if (root.val > high)
return trimBST(root.left, low, high);
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}
- 時間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1),忽略遞歸的額外空間開銷
C++
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (root == nullptr)
return nullptr;
if (root->val < low)
return trimBST(root->right, low, high);
else if (root->val > high)
return trimBST(root->left, low, high);
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
- 時間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1),忽略遞歸的額外空間開銷
Rust
- 今天又見識到了新報錯:
already borrowed: BorrowMutError,不能把borrow的東西來回隨便等,要搞臨時中間變量,閉包要關(guān)好。
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn trim_bst(root: Option<Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> Option<Rc<RefCell<TreeNode>>> {
if root.is_none() {
return None;
}
if root.as_ref().unwrap().borrow().val < low {
return Solution::trim_bst(root.as_ref().unwrap().borrow().right.clone(), low, high);
}
else if root.as_ref().unwrap().borrow().val > high {
return Solution::trim_bst(root.as_ref().unwrap().borrow().left.clone(), low, high);
}
let (l, r) = (Solution::trim_bst(root.as_ref().unwrap().borrow().left.clone(), low, high), Solution::trim_bst(root.as_ref().unwrap().borrow().right.clone(), low, high)); // 要先拎出來
root.as_ref().unwrap().borrow_mut().left = l;
root.as_ref().unwrap().borrow_mut().right = r;
root
}
}
- 時間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1),忽略遞歸的額外空間開銷
以上就是Java C++ 算法題解leetcode669修剪二叉搜索樹的詳細內(nèi)容,更多關(guān)于Java C++ 算法修剪二叉搜索樹的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實操之內(nèi)聯(lián)成員函數(shù)介紹
大家好,本篇文章主要講的是C++實操之內(nèi)聯(lián)成員函數(shù)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
C++數(shù)據(jù)結(jié)構(gòu)分析多態(tài)的實現(xiàn)與原理及抽象類
繼承就是可以直接使用前輩的屬性和方法。自然界如果沒有繼承,那一切都是處于混沌狀態(tài)。多態(tài)是同一個行為具有多個不同表現(xiàn)形式或形態(tài)的能力。多態(tài)就是同一個接口,使用不同的實例而執(zhí)行不同操作2022-02-02

