寒假刷题打卡第十四天 | 树

这篇博客介绍了如何将有序链表转化为平衡二叉搜索树,利用递归方法实现。同时讲解了在二叉搜索树中查找元素和计算最小绝对差值的方法,并给出了中序遍历在解决此类问题中的应用。此外,还涉及到了二叉搜索树中找众数的算法,同样基于中序遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 有序链表转换为平衡搜索二叉树
    联系有序数组转换为平衡二叉树的解法,显然我们可以先把整个链表存放到一个数组中。
    不过答案给了一个更巧妙的解法
class Solution {
public:
    int getLength(ListNode* head) {
        int ret = 0;
        for (; head != nullptr; ++ret, head = head->next);
        return ret;
    }

    TreeNode* buildTree(ListNode*& head, int left, int right) {
        if (left > right) {
            return nullptr;
        }
        int mid = (left + right + 1) / 2;
        TreeNode* root = new TreeNode();
        root->left = buildTree(head, left, mid - 1);
        root->val = head->val;
        head = head->next;
        root->right = buildTree(head, mid + 1, right);
        return root;
    }

    TreeNode* sortedListToBST(ListNode* head) {
        int length = getLength(head);
        return buildTree(head, 0, length - 1);
    }
};
  1. 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。
    这是一道简单题。没错,就是你想的 那样做。

  2. 二叉搜索树的最小绝对差
    这道题很容易想到中序遍历。但是如何给lastVal和minAbs赋初值需要注意一下。

class Solution {  // 21:20 - 21:39
public:
    int lastVal, minAbs;
    int just_start = 1;
    int getMinimumDifference(TreeNode* root) {
        getMinimumDifferenceCore(root);
        return minAbs;
    }
    void getMinimumDifferenceCore(TreeNode* root)
    {
        if(!root)
            return;
        getMinimumDifferenceCore(root->left);
        if(just_start)
        {
            just_start--;
            lastVal = root->val;
            minAbs = 9999999999;
        }
        //else if(just_start==1)
        //{
        //    just_start--;
        //    minAbs = root->val - lastVal;  //也可以一开始把它初始化为一个很大的数
        //    lastVal = root->val;
        //}
        else
        {
            minAbs = min(minAbs, abs(root->val - lastVal));
            lastVal = root->val;
        }
        getMinimumDifferenceCore(root->right);
    }
};
  1. 二叉搜索树中的众数
    和上一道题一样,中序遍历。
class Solution {
public:
    int lastVal, maxNum=0, startFlag=1, thisNum=0;
    vector<int> ans;
    vector<int> findMode(TreeNode* root) {
        findModeCore(root);
        return ans;
    }
    void findModeCore(TreeNode* root)
    {
        if(!root)
            return;
        findModeCore(root->left);
        if(startFlag)
        {
            startFlag--;
            lastVal = root->val;
            maxNum = 1;
            ans.push_back(root->val);
            thisNum = 1;
        }
        else
        {
            if(root->val == lastVal)
            {
                thisNum++;
            }
            else
            {
                lastVal = root->val;
                thisNum=1;
            }
            if(thisNum==maxNum)                 //第一次提交的时候我把
                ans.push_back(root->val);       //这一部分只写在了
            else if(thisNum>maxNum)             //if(root->val == lastVal)
            {                                   //这个大括号里面
                vector<int> temp;               //错误地认为如果不等就不会有更新
                ans = temp;                     //实际上
                ans.push_back(root->val);       //当众数数量为1时,也需要更新
                maxNum = thisNum;
            }
        }
        findModeCore(root->right);
    }
};
  1. Trie树
    trie树的值就是它的next是否为null。
  2. 暂略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值