曾经沧海难为水,除却巫山不是云。|

Joey-Wang

园龄:4年3个月粉丝:17关注:0

9.4 二叉查找树

9.4 二叉查找树

http://codeup.hustoj.com/contest.php?cid=100000613

A 二叉排序树

题目解析

唯一的坑在于“输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。”
题目中没说。

代码

#include <cstdio>

struct node {
    int data;
    node *lchild;
    node *rchild;
};
int n;

void insert(node *&root, int x) { //注意&!!!
    // 找到插入位置
    if (root == NULL) {
        root = new node;
        root->data = x;
        root->lchild = root->rchild = NULL;
        return;
    }
    if (x == root->data) return;
    else if (x < root->data) {
        insert(root->lchild, x);
    } else {
        insert(root->rchild, x);
    }
}

node *create() {
    int temp;
    node *root = NULL;
    for (int i = 0; i < n; i++) {
        scanf("%d", &temp);
        insert(root, temp);
    }
    return root;
}

void preOrder(node *root) {
    if (root == NULL) return;
    printf("%d ", root->data);
    preOrder(root->lchild);
    preOrder(root->rchild);
}

void inOrder(node *root) {
    if (root == NULL) return;
    inOrder(root->lchild);
    printf("%d ", root->data);
    inOrder(root->rchild);
}

void postOrder(node *root) {
    if (root == NULL) return;
    postOrder(root->lchild);
    postOrder(root->rchild);
    printf("%d ", root->data);
}

int main() {
    while (scanf("%d", &n) != EOF) {
        node *root = create();
        preOrder(root);
        printf("\n");
        inOrder(root);
        printf("\n");
        postOrder(root);
        printf("\n");
    }
    return 0;
}

B 二叉搜索树

image-20200826201345691

题目解析

中序遍历+前序遍历/后序遍历才能唯一的确定一棵二叉树

二叉排序树 而言,相同元素的二叉排序树中序遍历一定相同,而不同元素二叉排序树使用前序遍历就可以发现不相同,所以只需要前序遍历两个二叉树,比较一下就可以判断

❗️“答案错误50”,看了半天不知道哪里错了。看了网上的代码,觉得写得没差别_(:з」∠)_

代码(答案错误50)

#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
struct node {
    int data;
    node *lchild;
    node *rchild;
};


void insert(node *&root, int x) {
    if (root == NULL) {
        root = new node;
        root->data = x;
        root->lchild = root->rchild = NULL;
        return;
    }
    if (x <= root->data) {
        insert(root->lchild, x);
    } else {
        insert(root->rchild, x);
    }
}

node *create(string data) {
    node *root = NULL;
    for (int i = 0; i < data.length(); i++) {
        insert(root, data[i] - '0');
    }
    return root;
}

//先序遍历
void preOrder(node *root, string &s) {
    if (root == NULL)return;
    s += root->data;
    preOrder(root->lchild, s);
    preOrder(root->rchild, s);
}

//中序遍历
void inOrder(node *root, string &s) {
    if (root == NULL) return;
    inOrder(root->lchild, s);
    s += root->data;
    inOrder(root->rchild, s);
}

int main() {
    int n;
    string s;
    string pre1, pre2, in1, in2;
    while (scanf("%d", &n)!=EOF && n != 0) {
        cin >> s;
        node *root = create(s);
        preOrder(root, pre1);
        inOrder(root, in1);
        while (n--) {
            pre2.clear();
            in2.clear();
            cin >> s;
            root = create(s);
            preOrder(root, pre2);
            inOrder(root, in2);
            if (pre1 == pre2 && in1 == in2) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

本文作者:Joey-Wang

本文链接:https://www.cnblogs.com/joey-wang/p/14541183.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Joey-Wang  阅读(64)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开