题解 | #二叉排序树#

二叉排序树

https://www.nowcoder.com/practice/30a0153649304645935c949df7599602

#include<iostream>
using namespace std;

typedef struct node{
	int val;
	struct node *left, *right;
}bittree, *tree;

//递归建树,并且返回父亲节点
int build(tree &t, tree node){
	if(t == NULL){
		t = node;
		return -1;
	}
	if(t->val > node->val){
		if(t->left == NULL){
			t->left = node;
			return t->val;
		}else{
			return build(t->left, node);
		}
	}else if(t->val < node->val){
		if(t->right == NULL){
			t->right = node;
			return t->val;
		}else{
			return build(t->right, node);
		}
	}else{
		return -1;
	}
}

int main(){
	int n, m;
	cin >> n;
	tree t = NULL;
	
	for(int i = 0; i < n; i++){
		cin >> m;
		tree node = (tree)malloc(sizeof(bittree));
		node->left = NULL;
		node->right = NULL;
		node->val = m;
		cout << build(t, node) << endl;
	}
	return 0;
}

全部评论

相关推荐

Rena1ssance_:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务