import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static class Node {
int value;
Node left;
Node right;
Node parent;
int height;
public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
this.parent = null;
this.height = 0;
}
}
static ArrayList<Node> edge_List = new ArrayList<>();
static Node Insert(Node node, int value, Node parent, int height) {
if (node == null) {
Node new_node = new Node(value);
new_node.parent = parent;
new_node.height = height;
return new_node;
}
if (value < node.value) {
node.left = Insert(node.left, value, node, height+1);
} else if (value > node.value) {
node.right = Insert(node.right, value, node, height+1);
} else {
// 이건 안해도 될 듯
}
return node;
}
static Node findNode(Node node, int value){
if (node == null || node.value==value) return node;
if (value < node.value) return findNode(node.left, value);
return findNode(node.right, value);
}
static void CollectLeaves(Node node){
if (node == null) return;
if (node.left == null && node.right == null) edge_List.add(node);
CollectLeaves(node.left);
CollectLeaves(node.right);
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
Node root = null;
for (int i=0; i<N; i++) {
int t = Integer.parseInt(st.nextToken());
root = Insert(root, t, null, 0);
}
int a = Integer.parseInt(br.readLine());
Node A = findNode(root, a);
CollectLeaves(root);
int L_height_gap = 0;
int R_height_gap = 0;
if (A != null) {
for (Node leaf : edge_List) {
Node cur = leaf;
while (cur != null) {
if (A.left != null && cur == A.left) {
L_height_gap = Math.max(L_height_gap, leaf.height - A.left.height+1);
}
if (A.right != null && cur == A.right) {
R_height_gap = Math.max(R_height_gap, leaf.height - A.right.height + 1);
}
cur = cur.parent;
}
}
}
System.out.println(L_height_gap);
System.out.println(R_height_gap);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: