亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java?C++?算法題解leetcode669修剪二叉搜索樹示例

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

題目要求

思路一:模擬迭代

  • 依次判斷每個(gè)節(jié)點(diǎn)是否合法:
    • 首先找出結(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;
    }
}
  • 時(shí)間復(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;
    }
};
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(1)

思路二:遞歸

  • 直接用當(dāng)前函數(shù)遞歸修剪即可:
    • 當(dāng)前值小了放右下(大)的值進(jìn)去,剪掉當(dāng)前和左邊節(jié)點(diǎn);
    • 當(dāng)前值大了放左下(?。┑闹颠M(jìn)去,剪掉當(dāng)前和右邊節(jié)點(diǎn)。
    • 然后遞歸掉下面所有節(jié)點(diǎn)。

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;
    }
}
  • 時(shí)間復(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;
    }
};
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(1),忽略遞歸的額外空間開銷

Rust

  • 今天又見識(shí)到了新報(bào)錯(cuò):already borrowed: BorrowMutError,不能把borrow的東西來回隨便等,要搞臨時(shí)中間變量,閉包要關(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
    }
}
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(1),忽略遞歸的額外空間開銷

以上就是Java C++ 算法題解leetcode669修剪二叉搜索樹的詳細(xì)內(nèi)容,更多關(guān)于Java C++ 算法修剪二叉搜索樹的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++中vector的模擬實(shí)現(xiàn)實(shí)例詳解

    C++中vector的模擬實(shí)現(xiàn)實(shí)例詳解

    vector是表示可變大小數(shù)組的序列容器,它也采用連續(xù)存儲(chǔ)空間來存儲(chǔ)元素,因此可以采用下標(biāo)對(duì)vector的元素進(jìn)行訪問,這篇文章主要給大家介紹了關(guān)于C++中vector模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 一篇文章讓你徹底明白c++11增加的變參數(shù)模板

    一篇文章讓你徹底明白c++11增加的變參數(shù)模板

    C++11的新特性--可變模版參數(shù)(variadic templates)是C++11新增的最強(qiáng)大的特性之一,它對(duì)參數(shù)進(jìn)行了高度泛化,它能表示0到任意個(gè)數(shù)、任意類型的參數(shù),這篇文章主要給大家詳細(xì)介紹了關(guān)于c++11增加的變參數(shù)模板的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • C++實(shí)操之內(nèi)聯(lián)成員函數(shù)介紹

    C++實(shí)操之內(nèi)聯(lián)成員函數(shù)介紹

    大家好,本篇文章主要講的是C++實(shí)操之內(nèi)聯(lián)成員函數(shù)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 一文解析C語言中動(dòng)態(tài)內(nèi)存管理

    一文解析C語言中動(dòng)態(tài)內(nèi)存管理

    這篇文章主要為大家詳細(xì)介紹了C語言中動(dòng)態(tài)內(nèi)存管理的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • C語言編程使用MATLAB繪制橢圓及圓角矩形

    C語言編程使用MATLAB繪制橢圓及圓角矩形

    這篇文章主要為大家介紹了C語言編程中使用MATLAB繪制橢圓及圓角矩形的實(shí)現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • C++數(shù)據(jù)結(jié)構(gòu)分析多態(tài)的實(shí)現(xiàn)與原理及抽象類

    C++數(shù)據(jù)結(jié)構(gòu)分析多態(tài)的實(shí)現(xiàn)與原理及抽象類

    繼承就是可以直接使用前輩的屬性和方法。自然界如果沒有繼承,那一切都是處于混沌狀態(tài)。多態(tài)是同一個(gè)行為具有多個(gè)不同表現(xiàn)形式或形態(tài)的能力。多態(tài)就是同一個(gè)接口,使用不同的實(shí)例而執(zhí)行不同操作
    2022-02-02
  • C++的字符串分割函數(shù)的使用詳解

    C++的字符串分割函數(shù)的使用詳解

    本篇文章主要介紹了C++的字符串分割函數(shù),主要用strtok、STL、Boost進(jìn)行字符串分割,有需要的可以了解一下。
    2016-11-11
  • C語言學(xué)習(xí)筆記之VS2022安裝使用教程

    C語言學(xué)習(xí)筆記之VS2022安裝使用教程

    這篇文章主要介紹了C語言學(xué)習(xí)筆記之VS2022安裝使用教程,在VS2022中,在使用scanf函數(shù)編譯出錯(cuò),本文給大家提到了解決方法,需要的朋友可以參考下
    2022-05-05
  • linux下c語言的多線程編程

    linux下c語言的多線程編程

    這篇文章主要介紹了linux下c語言的多線程編程,需要的朋友可以參考下
    2017-10-10
  • C/C++從零開始的cmake教程

    C/C++從零開始的cmake教程

    今天小編就為大家分享一篇關(guān)于C/C++從零開始的cmake教程,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10

最新評(píng)論