【数据结构】二叉搜索树(二叉排序树、二叉查找树)BST(Binary Search Tree)专题

判断某序列是否为BST的后序遍历

46. 二叉搜索树的后序遍历序列

class Solution {
public:
    vector<int> seq;
    bool verifySequenceOfBST(vector<int> sequence) {
        seq = sequence;
        return dfs(0, seq.size() - 1);
    }

    bool dfs(int l, int r)
    {
        if (l >= r) return true;
        int root = seq[r]; // 后序遍历的最后一个元素为根节点
        int k = l;
        while (k < r && seq[k] < root) k ++ ;
        for (int i = k; i < r; i ++ )
            if (seq[i] < root) 
                return false;

        return dfs(l, k - 1) && dfs(k, r - 1);
    }
};

构建BST并前、中、后序遍历

3540. 二叉搜索树

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 110;

int w[N], l[N], r[N], idx;
int n, root;

void insert(int& u, int x)
{
    if (!u) u = ++ idx, w[u] = x;
    else if (x < w[u]) insert(l[u], x); // 插到左子树
    else if (x > w[u]) insert(r[u], x); // 插到右子树
}

void dfs(int u, int t) // u为根节点,t表示类型,0为前序,1为中序,2为后序
{
    if (!u) return;
    if (t == 0) cout << w[u] << ' ';
    dfs(l[u], t);
    if (t == 1) cout << w[u] << ' ';
    dfs(r[u], t);
    if (t == 2) cout << w[u] << ' ';
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ )
    {
        int x;
        cin >> x;
        insert(root, x);
    }
    
    for (int i = 0; i < 3; i ++ )
    {
        dfs(root, i); // 第二个参数表示前、中、后序遍历
        cout << endl;
    }
    return 0;
}

构建BST(指针写法),实现插入,删除,并查找目标值前驱和后继结点

3786. 二叉排序树

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int INF = 1e8;

struct TreeNode
{
    int val;
    TreeNode *left, *right;
    TreeNode(int _val): val(_val), left(NULL), right(NULL) {} 
}*root;

void insert(TreeNode* &root, int x)
{
    if(!root) root = new TreeNode(x);
    else if(x < root->val) insert(root->left, x);
    else if(x > root->val) insert(root->right, x);
}

void remove(TreeNode* &root, int x)
{
    if(!root) return;
    if(x < root->val) remove(root->left, x);
    else if(x > root->val) remove(root->right, x);
    else 
    {
        if(!root->left && !root->right) root = NULL;
        else if(!root->left) root = root->right;
        else if(!root->right) root = root->left;
        else 
        {
            auto p = root->left;
            while(p->right) p = p->right;
            root->val = p->val;
            remove(root->left, p->val);
        }
    }
}

int get_pre(TreeNode* root, int x)
{
    if(!root) return -INF;
    if(root->val >= x) return get_pre(root->left, x);
    return max(root->val, get_pre(root->right, x));
}

int get_suc(TreeNode* root, int x)
{
    if(!root) return INF;
    if(root->val <= x) return get_suc(root->right, x);
    return min(root->val, get_suc(root->left, x));
}

int main()
{
    int n;
    cin >> n;
    while(n -- )
    {
        int t, x;
        cin >> t >> x;
        if(t == 1) insert(root, x);
        else if(t == 2) remove(root, x);
        else if(t == 3) cout << get_pre(root, x) << endl;
        else cout << get_suc(root, x) << endl; 
    }
    return 0;
}
posted @   Tshaxz  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
历史上的今天:
2021-09-03 【数据结构】Trie树
2021-09-03 【图论之差分约束】差分约束专题
Language: HTML
点击右上角即可分享
微信分享提示