/* Author : Nguyen Thanh Tung */
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int, int> ii;
const int N = 1e5 + 7;
const long long oo = 1e18 + 7;
const long long MOD = 1e9 + 7;
int n;
struct node {
int key;
struct node *left;
struct node *right;
};
typedef struct node * nodeTree;
nodeTree t;
void khoitao(nodeTree &t) {
t = NULL;
}
nodeTree newNode() {
nodeTree p = (nodeTree)malloc(sizeof(struct node));
return p;
}
nodeTree insertNode(nodeTree &p, int x)
{
if (p == NULL) {
p = newNode();
p -> key = x;
p -> left = p -> right = NULL;
}
else if (x < p->key)
p->left = insertNode(p->left, x);
else if (x > p->key)
p->right = insertNode(p->right, x);
return p;
}
void nhap(nodeTree &t) {
cin >> n;
// t = insertNode(t, x);
for(int i = 1; i <= n; ++i) {
int x;
cin >> x;
insertNode(t, x);
}
}
void nlrdq(nodeTree t) {
if(t != NULL) {
cout << (t -> key) << " ";
nlrdq(t -> left);
nlrdq(t -> right);
}
}
void nlrst(nodeTree t) {
if (t == NULL) return;
stack<nodeTree> s;
s.push(t);
while (!s.empty()) {
nodeTree current = s.top();
s.pop();
cout << current->key << " ";
if (current->right != NULL) {
s.push(current->right);
}
if (current->left != NULL) {
s.push(current->left);
}
}
}
void lnrdq(nodeTree t) {
if(t != NULL) {
lnrdq(t -> left);
cout << (t -> key) << " ";
lnrdq(t -> right);
}
}
void lnrst(nodeTree t) {
stack<nodeTree> s;
nodeTree current = t;
while (current != NULL || !s.empty()) {
while (current != NULL) {
s.push(current);
current = current->left;
}
current = s.top();
s.pop();
cout << current->key << " ";
current = current->right;
}
}
void lrndq(nodeTree t) {
if(t != NULL) {
lrndq(t -> left);
lrndq(t -> right);
cout << (t -> key) << " ";
}
}
void lrnst(nodeTree t) {
if (t == NULL) return;
stack<nodeTree> s;
stack<nodeTree> p;
s.push(t);
while (!s.empty()) {
nodeTree current = s.top();
s.pop();
p.push(current);
if (current->left != NULL) {
s.push(current->left);
}
if (current->right != NULL) {
s.push(current->right);
}
}
while (!p.empty()) {
cout << p.top()->key << " ";
p.pop();
}
}
void duyetmuc(nodeTree t) {
if(t == NULL) {
return;
}
queue<nodeTree> st;
st.push(t);
while(!st.empty()) {
nodeTree cur = st.front();
st.pop();
cout << cur -> key << " ";
if(cur -> left != NULL) {
st.push(cur -> left);
}
if(cur -> right != NULL) {
st.push(cur -> right);
}
}
}
void tv1() {
cout <<"1/ " << endl;
cout << "NLR : ";
nlrst(t);
cout << endl;
cout << "LNR : ";
lnrst(t);
cout << endl;
cout << "LRN : ";
lrnst(t);
cout << endl;
cout << "Duyet Muc : ";
duyetmuc(t);
cout << endl;
}
int nct(nodeTree t, int cnt) {
if(t -> left == NULL) return cnt;
nct(t -> left, cnt + 1);
}
int ncp(nodeTree t, int cnt) {
if(t -> right == NULL) return cnt;
ncp(t -> right, cnt + 1);
}
void tv2() {
cout << "2/ ";
cout << nct(t, 0) << endl;
}
void tv3() {
cout << "3/ ";
cout << ncp(t, 0) << endl;
}
int dem_node(nodeTree t) {
int Left;
if(t -> left == NULL) {
Left = 1;
}
else {
Left = dem_node(t -> left) + 1;
}
int Right;
if(t -> right == NULL) {
Right = 1;
}
else {
Right = dem_node(t -> right) + 1;
}
return Right + Left - 1;
}
void tv4() {
cout << "4/ ";
cout << dem_node(t -> left) << endl;
}
void tv5() {
cout << "5/ ";
cout << dem_node(t -> right) << endl;
}
int dem_1_ccon(nodeTree t) {
if(t -> right == NULL && t -> left == NULL)
return 0;
if(t -> right == NULL)
return dem_1_ccon(t -> left) + 1;
if(t -> left == NULL)
return dem_1_ccon(t -> right) + 1;
if(t -> left != NULL && t -> right != NULL)
return dem_1_ccon(t -> left) + dem_1_ccon(t -> right);
}
void tv6() {
cout << "6/ " << dem_1_ccon(t) << endl;
}
int dem_la(nodeTree t) {
if(t == NULL)
return 0;
if(t -> right == NULL && t -> left == NULL)
return 1;
return dem_la(t -> right) + dem_la(t -> left);
}
void tv7() {
cout << "7/ " << dem_la(t) << "\n";
}
int ccao(nodeTree t) {
if(t -> left == NULL && t -> right == NULL)
return 1;
if(t -> left == NULL)
return ccao(t -> right) + 1;
if(t -> right == NULL)
return ccao(t -> left) + 1;
return max(ccao(t -> right), ccao(t -> left)) + 1;
}
void tv8() {
cout << "8/ " << ccao(t) << "\n";
}
int maxx(nodeTree t) {
if(t -> right == NULL) {
return t -> key;
}
return maxx(t -> right);
}
void tv9() {
cout << "9/ " << maxx(t) << endl;
}
void solve() {
khoitao(t);
nhap(t);
tv1();
tv2();
tv3();
tv4();
tv5();
tv6();
tv7();
tv8();
tv9();
}
#define TASK "code"
signed main () {
ios_base::sync_with_stdio (false);
cin.tie(nullptr), cout.tie(nullptr);
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}