最后
学习视频:
大厂面试真题:
根据输入的序列进行建树 输出遍历顺序
3:(如果上诉否 那么就用这个)解决镜像二叉搜索树
利用层序遍历(将输出的顺序 改为先输出右结点 再输出左结点)
得到层序顺序后根据 层序顺序建树
3(2.0):写完前面的码后感觉 再写一个建树有点麻烦 重新捋捋
镜像 就是将前序先输出左改成右边 同时后序也是
4:如果都否的话 输出 NO
*/
#include<bits/stdc++.h>
using namespace std;
typedef struct TNode*Ptrtree;
typedef struct TNode{
int Data;
Ptrtree left;
Ptrtree right;
}tnode;
int N;
vectorv1,v2,v3,v4;
//建立二叉搜索树
Ptrtree insert(Ptrtree root,int x){
//插入完第一个后 根节点也就固定了
if(root == NULL){//将插入的操作视为 查找的时的操作,插入的地点视为 查找失败的地点 在查找失败的地点 插入一个结点
root = (Ptrtree)malloc(sizeof(struct TNode));
root->left = NULL;
root->right = NULL;
root->Data = x;
return root;
}
if(root->Data > x){
root->left = insert(root->left,x);
}
else if(root->Data <= x){
root->right = insert(root->right,x);
}
else{
return NULL;
}
return root;
}
Ptrtree creatTree(int A[],Ptrtree root){
root = NULL;
int i;
for(i = 0; i < N; i++){
root = insert(root,A[i]);
}
return root;
}
//二叉搜索树的前序遍历
void Preorder1( Ptrtree root )
{
if( root != NULL)
{
int temp = root->Data;
v1.push_back(temp);
Preorder1(root->left);
Preorder1(root->right);
}
}
//二叉搜索树的后序遍历
void Postorde1(Ptrtree root)
{
if(root != NULL)
{
Postorde1(root->left);
Postorde1(root->right);
int temp = root->Data;
v2.push_back(temp);
}
}
//二叉镜像树的前序遍历
void Preorder2( Ptrtree root )
{
if( root != NULL)
{
int temp = root->Data;
v3.push_back(temp);
Preorder2(root->right);
Preorder2(root->left);
}
}
//二叉镜像树的后序遍历
void Postorde2(Ptrtree root)
{
if(root != NULL)
{
Postorde2(root->right);
Postorde2(root->left);
int temp = root->Data;
v4.push_back(temp);
}
}
//比较两个容器当中的数据是否相等
int judgment( int a[],vector&v)
{
int flag = 0;
for(int i = 0; i < N; i++)
{
if(a[i] != v[i])
{
flag = 1;
break;
}
}
if( flag == 0)
return 1;
else
return 0;
}
int main(){
int a[1000];
cin >> N;
for(int i = 0; i < N; i++)
{
cin >> a[i];
}
Ptrtree root;
root = creatTree(a,root);
Preorder1(root);
int flag1 = judgment(a,v1); //判断二叉搜索树的前序是否正确
//如果是二叉搜索树的前序序列 正确
if( flag1 == 1 )
{
cout << “YES” << endl;
Postorde1(root);
for( int i = 0; i < v2.size(); i++ )
{
if( i != N-1)
cout << v2[i] << ’ ';
else
cout << v2[i];
}
}else
{
//比较是否是镜像的二叉树
Preorder2(root);
int flag2 = judgment(a,v3);
if( flag2 == 1 )
{
cout << “YES” << endl;
Postorde(root);
for( int i = 0; i < N; i++ )
总结
以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的面试大全资料(含JAVA、MySQL、算法、Redis、JVM、架构、中间件、RabbitMQ、设计模式、Spring等),现在起闭关修炼半个月,争取早日上岸!!!
下面给大家分享下我的面试大全资料
- 第一份是我的后端JAVA面试大全
后端JAVA面试大全
- 第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理
- 第三份是Spring全家桶资料
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理
M+JAVA核心知识整理
[外链图片转存中…(img-YwsB1zkm-1715799726168)]
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理
- 第三份是Spring全家桶资料
[外链图片转存中…(img-dZgPrPNX-1715799726169)]
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理