文章

85

粉丝

0

获赞

579

访问

14.6k

头像
二叉排序树 - 华科 题解:带父节点指针的二叉搜索树
P1396 华中科技大学
发布于2026年3月6日 13:41
阅读数 120

#include <bits/stdc++.h>
using namespace std;
typedef struct Node {
    int data;
    struct Node* left;struct Node* right;
    struct Node* parent;
}*STree;
void buildSTree(STree &root,int data,STree parent) {
    if (root==NULL) {
        root =new Node;
        root->data = data;
        root->left = NULL;
        root->right = NULL;
        root->parent = parent;
        if (root->parent==NULL) {
            cout <<-1<<endl;
        }
        else {
            cout <<root->parent->data<<endl;
        }
        return;
    }
    if (data==root->data) {
        return;
    }
    if (data<root->data) {
        buildSTree(root->left,data,root);
    }
    if (data>root->data) {
        buildSTree(root->right,data,root);
    }
}


int main() {
    int n;
    while (cin>>n) {
        vector<int> v;
        for (int i=0;i<n;i++) {
            int data;
  ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发