zoukankan      html  css  js  c++  java
  • 数据结构与算法题目集(中文)7-28 搜索树判断 (25分) 递归与数组的灵活运用

    1.题目

    对于二叉搜索树,我们规定任一结点的左子树仅包含严格小于该结点的键值,而其右子树包含大于或等于该结点的键值。如果我们交换每个节点的左子树和右子树,得到的树叫做镜像二叉搜索树。

    现在我们给出一个整数键值序列,请编写程序判断该序列是否为某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,如果是,则输出对应二叉树的后序遍历序列。

    输入格式:

    输入的第一行包含一个正整数N(≤1000),第二行包含N个整数,为给出的整数键值序列,数字间以空格分隔。

    输出格式:

    输出的第一行首先给出判断结果,如果输入的序列是某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,则输出YES,否侧输出NO。如果判断结果是YES,下一行输出对应二叉树的后序遍历序列。数字间以空格分隔,但行尾不能有多余的空格。

    输入样例1:

    7
    8 6 5 7 10 8 11
    

    输出样例1:

    YES
    5 7 6 8 11 10 8
    

    输入样例2:

    7
    8 6 8 5 10 9 11
    

    输出样例2:

    NO

    2.题目分析

    1.二叉搜索树寻找右子树:从当前头节点向后找到比头节点大的位置 i 之后开始递归 出错的位置就是位置i之后出现比头节点小或等的值

    2.镜像二叉树寻找右子树:从当前头节点向后找到比头节点小或等的位置 i 之后开始递归 错的位置就是位置i之后出现比头节点小的值

    3.注意事项

    在递归中的参数传递时传递的数组的开始指针,这样递归的时候很方便

    3.代码

    #include<iostream>
    using namespace std;
    
    struct node
    {
    	int data;
    	struct node *left;
    	struct node *right;
    };
    typedef struct node *List;
    bool isfind1 = true;
    List find(int aaa[],int n)
    {
    	if (n == 0)
    	{
    		isfind1 = true;
    		return NULL;
    	}
    	List temp = (List)malloc(sizeof(struct node));
    	temp->left = temp->right = NULL;
    	temp->data = *aaa;//注意
    	int i;
    	for (i = 1; i < n; i++)
    	{
    		if (temp->data <= aaa[i])
    			break;
    	}
    	for (int j = i; j < n; j++)
    	{
    		if (aaa[j] < temp->data)
    		{
    			isfind1 = false;
    			return NULL;
    		}
    	}
    	temp->left = find(aaa + 1, i-1);//注意
    	temp->right = find(aaa + i , n - i);//注意
    	return temp;
    }
    
    
    bool isfind2 = true;
    List find2(int aaa[], int n)
    {
    	if (n == 0)
    	{
    		isfind2 = true;
    		return NULL;
    	}
    
    	List temp = (List)malloc(sizeof(struct node));
    	temp->left = temp->right = NULL;
    	temp->data = *aaa;
    	int i;
    	for (i = 1; i < n; i++)
    	{
    		if (temp->data > aaa[i])
    			break;
    	}
    	for (int j = i; j < n; j++)
    	{
    		if (aaa[j] >= temp->data)
    		{
    			isfind2 = false;
    			return NULL;
    		}
    	}
    	temp->left = find2(aaa + 1, i - 1);//
    	temp->right = find2(aaa + i, n - i);//
    	return temp;
    }
    
        void afterorder(List t,int &count)
        {
            if (t == NULL)
                return;
            afterorder(t->left,count);
            afterorder(t->right,count);
            if (count == 0)
            {
                cout << "YES" << endl;
                cout << t->data;
                count++;
            }
            else
                cout << " " << t->data;
    
        }
    
    int main()
    {
    	int n;
    	cin >> n;
    	int aaa[1001];
    	for (int i = 0; i < n; i++)
    	{
    		cin >> aaa[i];
    	}
    	List t= NULL;
    	List tt = NULL;
    	t = find(aaa, n);
    
    	int count = 0;
    	if (isfind1 == true)	
    		afterorder(t, count);
    	if (isfind1 == false)
    	{
    		tt = find2(aaa, n);
    		if (isfind2 == true)
    			afterorder(tt, count);
    		else
    			cout << "NO" << endl;
    	}
    
    }
  • 相关阅读:
    centos 7 安装mqtt 修改用户名和密码
    5-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(,远程升级GPRS内部程序)
    ESP8266开发综合篇第十四节(LUA)-8266作为TCP服务器,Android客户端连接,显示温湿度,控制继电器
    ESP8266开发综合篇第一节(LUA)-下载和刷固件
    springMvc源码学习之:spirngMvc的拦截器使用
    Java Web学习(1): 客户端请求、服务器响应及其HTTP状态码
    HttpServletRequest常用获取URL的方法
    java多线程:并发包中的信号量和计数栓的编程模型
    springMvc源码学习之:利用springMVC随时随地获取HttpServletRequest等对象
    springMvc源码学习之:spirngMvc的参数注入的问题
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12789009.html
Copyright © 2011-2022 走看看