亲宝软件园·资讯

展开

Java C++ 修剪二叉搜索树

AnjaVon 人气:0

题目要求

思路一:模拟迭代

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;
    }
}

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;
    }
};

思路二:递归

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;
    }
}

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;
    }
};

Rust

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
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论