1043. Is It a Binary Search Tree (25)
来源:互联网 发布:sql2005 mdf恢复数据库 编辑:程序博客网 时间:2024/06/06 11:37
笨方法,勉强看一看吧
#define _CRT_SECURE_NO_WARNINGS
#include<string>#include<string.h>
#include<vector>
#include<map>
#include<stack>
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Node
{
int num;
Node *left, *right;
Node()
{
left = right = NULL;
}
};
bool InsertNode(Node *seq_left, int i, int N)
{
Node *root = seq_left;
while (N--)
{
if (seq_left[i].num < root->num)
{
//插入树的左边
if (root->right != NULL)
return false;
if (root->left == NULL)
{
root->left = &seq_left[i];
return true;
}
else
{
root = root->left;
}
}
else//插入树的右边
{
if (root->right == NULL)
{
root->right = &seq_left[i];
return true;
}
else
{
root = root->right;
}
}
}
return false;
}
bool InsertNodeMirror(Node *seq_left, int i, int N)
{
Node *root = seq_left;
while (N--)
{
if (seq_left[i].num >= root->num)
{
//插入树的左边
if (root->right != NULL)
return false;
if (root->left == NULL)
{
root->left = &seq_left[i];
return true;
}
else
{
root = root->left;
}
}
else//插入树的右边
{
if (root->right == NULL)
{
root->right = &seq_left[i];
return true;
}
else
{
root = root->right;
}
}
}
return false;
}
void DisPlay(Node *seq,bool end=false)
{
if (seq->left != NULL)
DisPlay(seq->left);
if (seq->right != NULL)
DisPlay(seq->right);
if (end)
cout << seq->num;
else
cout << seq->num << " ";
}
int main()
{
int N;
cin >> N;
int *number = new int[N];
for (int i = 0; i < N; i++)
{
scanf("%d", &number[i]);
}
//查找树
Node *seq_left = new Node[N];
seq_left[0].num = number[0];
seq_left[0].left = seq_left[0].right = NULL;
bool tree = true;
for (int i = 1; i < N; i++)
{
seq_left[i].num = number[i];
if (!InsertNode(seq_left, i, N))
{
tree = false;
break;
}
}
if (!tree)
{
tree = true;
Node *seq_mirr = new Node[N];
seq_mirr[0].num = number[0];
seq_mirr[0].left = seq_mirr[0].right = NULL;
for (int i = 1; i < N; i++)
{
seq_mirr[i].num = number[i];
if (!InsertNodeMirror(seq_mirr, i, N))
{
tree = false;
break;
}
}
if (!tree)
{
cout << "NO\n";
}
else
{
cout << "YES\n";
DisPlay(seq_mirr,true);
}
}
else
{
cout << "YES\n";
DisPlay(seq_left,true);
}
return 0;
}
0 0
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043.Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- PAT A 1043. Is It a Binary Search Tree (25)
- PAT-A-1043. Is It a Binary Search Tree (25)
- pat-a 1043. Is It a Binary Search Tree (25)
- Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)—PAT
- 3dmax入门
- 算法基础:整数拆分问题(Golang实现)
- android 滑动导航效果FragmentTabViewPager
- POJ 3458 && HDU 1683 Colour Sequence(水~)
- When Programmers and Testers Collaborate
- 1043. Is It a Binary Search Tree (25)
- 机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size
- uboot relocation 重定位的总结与分析
- 使用Xcode和Instruments调试解决iOS内存泄露
- win7 IIS7.5/7.0 配置伪静态
- HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- Write Code As If You Had to Support It for the Rest of Your Life
- HDU 2102 A计划
- C++的global data的位置 及PE 文件中的section的内容