美文网首页
669.trim-a-binary-search-tree(do

669.trim-a-binary-search-tree(do

作者: Optimization | 来源:发表于2020-05-26 15:48 被阅读0次
问题:

1.为什么把其他不用的节点删除会有问题->问题是内存泄漏,没想清楚->不能delete(韩总).
2.为什么能把节点删除呢?->用简单的例子来试一下
3.为什么把把断的进行桥接呢?

正文:
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if(!root) return root;
        if(root->val < L) {
            TreeNode* result = trimBST(root->right, L, R);
            // deleteNode(root->left);
            // delete root;
            // root = nullptr;
            return result;
        } else if(root->val > R) {
            TreeNode* result = trimBST(root->left, L, R);
            // deleteNode(root->right);
            // delete root;
            // root = nullptr;
            return result;
        } else {
            root->left = trimBST(root->left, L, R);
            root->right = trimBST(root->right, L, R);
            return root;
        }   
    }
private:
    void deleteNode(TreeNode* root){
        if(!root) return;
        deleteNode(root->left);
        deleteNode(root->right);
        delete root;
        root = nullptr;
    }
};

相关文章

  • 669.trim-a-binary-search-tree(do

    问题: 1.为什么把其他不用的节点删除会有问题->问题是内存泄漏,没想清楚->不能delete(韩总).2.为什么...

  • 歌声暖我心【徐小凤】

    Do Do Do Do Do Do Do Do 歌声轻轻耳边哼 Do Do Do Do Do Do Do Do 歌...

  • to do or not to do

    日更第4天。 一生中,我们会面临很多选择。选一所学校,学一门专业,选一份工作,甚至,选一座城市,择一人终老。不论是...

  • DO IT DO IT

    只要行动,人生的美好会时不时蹦出来给你一次热烈地拥抱。真的! 今天休息日,也是感悟充实快乐的一天。早上六点多醒来,...

  • To do or not to do

    我们的生活总是面临着各种选择! 中考选高中,高考选大学,大学毕业选城市,城市落定选行业,行业明确选企业...

  • 2018-05-20

    Always getting over you其实还是很好听的嘛 ~ do do do do do do

  • Do or do not!There is no try!

    还没来得及看邓超导演的新片《银河补习班》。从朋友圈传来的这句台词就戳心了:“要么去做要么放弃,不要说我试试...

  • 有为与无为

    must to do/have to do,孔子的有为与无为。 Do what you have to do , ...

  • 018 (to) do up (clothes)

    含义 穿上... 举例 do up zipper do up jacket do up the tie do up...

  • 2018 -12 -19 做事务求到底的重要性

    前言: whatever you do, do it to the purpose; do it thr...

网友评论

      本文标题:669.trim-a-binary-search-tree(do

      本文链接:https://www.haomeiwen.com/subject/atkvahtx.html