自己写了个查找最大值和最小值的代码,不知道为什么没有输出,求大佬调QAQ
代码如下:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
struct Node {
int data;
Node *l;
Node *r;
};
Node *root;
Node *build(int data , Node *root) {
if(root == NULL) {
Node *newnode = new Node;
newnode -> data = data;
newnode -> l = NULL;
newnode -> r = NULL;
root = newnode;
return root;
}
if(root -> data < data)
root -> r = build(data , root -> r);
if(root -> data > data)
root -> l = build(data , root -> l);
return root;
}
void find1(Node *root) {
while(root -> l != NULL)
root = root -> l;
cout << root -> data << '\n';
return;
}
void find2(Node *root) {
while(root -> r != NULL)
root = root -> r;
cout << root -> data << '\n';
return;
}
signed main() {
cin >> n;
root = new Node;
root = NULL;
for(int i = 1;i <= n;i ++) {
int x;
cin >> x;
build(x , root);
}
find1(root);
find2(root);
return 0;
}