import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
BinTree tree=setupTree();
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line);
HashSet<Integer> hs=getPathHash(tree, Integer.parseInt(st.nextToken()));
LinkedList<Integer> list=getPathList(tree, Integer.parseInt(st.nextToken()));
boolean flag=false;
while(!list.isEmpty()){
int val=list.removeLast();
if(hs.contains(val)){
sb.append(val);
flag=true;
break;
}
}
if(flag)
sb.append('\n');
}
System.out.print(sb);
}
static BinTree setupTree(){
BinTree tree =new BinTree(30);
BinTree temp=tree;
temp.setLeft(8);
temp.setRight(52);
temp=temp.getLeft();
temp.setLeft(3);
temp.setRight(20);
temp=temp.getRight();
temp.setLeft(10);
temp.setRight(29);
return tree;
}
static HashSet<Integer> getPathHash(BinTree b,int value){
HashSet<Integer> hs=new HashSet<Integer>();
BinTree temp=b;
while(temp.getValue()!=value){
hs.add(temp.getValue());
if(temp.getValue()>value){
temp=temp.getLeft();
}else{
temp=temp.getRight();
}
}
hs.add(value);
return hs;
}
static LinkedList<Integer> getPathList(BinTree b,int value){
LinkedList<Integer> list=new LinkedList<Integer>();
BinTree temp=b;
while(temp.getValue()!=value){
list.add(temp.getValue());
if(temp.getValue()>value){
temp=temp.getLeft();
}else{
temp=temp.getRight();
}
}
list.add(value);
return list;
}
}
class BinTree{
private BinTree right;
private BinTree left;
private int value;
public BinTree(int x) {
value=x;
right=null;
left=null;
}
public BinTree getLeft() {
return left;
}
public void setLeft(int left) {
this.left = new BinTree(left);
}
public BinTree getRight() {
return right;
}
public void setRight(int right) {
this.right = new BinTree(right);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
BinTree tree=setupTree();
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line);
HashSet<Integer> hs=getPathHash(tree, Integer.parseInt(st.nextToken()));
LinkedList<Integer> list=getPathList(tree, Integer.parseInt(st.nextToken()));
boolean flag=false;
while(!list.isEmpty()){
int val=list.removeLast();
if(hs.contains(val)){
sb.append(val);
flag=true;
break;
}
}
if(flag)
sb.append('\n');
}
System.out.print(sb);
}
static BinTree setupTree(){
BinTree tree =new BinTree(30);
BinTree temp=tree;
temp.setLeft(8);
temp.setRight(52);
temp=temp.getLeft();
temp.setLeft(3);
temp.setRight(20);
temp=temp.getRight();
temp.setLeft(10);
temp.setRight(29);
return tree;
}
static HashSet<Integer> getPathHash(BinTree b,int value){
HashSet<Integer> hs=new HashSet<Integer>();
BinTree temp=b;
while(temp.getValue()!=value){
hs.add(temp.getValue());
if(temp.getValue()>value){
temp=temp.getLeft();
}else{
temp=temp.getRight();
}
}
hs.add(value);
return hs;
}
static LinkedList<Integer> getPathList(BinTree b,int value){
LinkedList<Integer> list=new LinkedList<Integer>();
BinTree temp=b;
while(temp.getValue()!=value){
list.add(temp.getValue());
if(temp.getValue()>value){
temp=temp.getLeft();
}else{
temp=temp.getRight();
}
}
list.add(value);
return list;
}
}
class BinTree{
private BinTree right;
private BinTree left;
private int value;
public BinTree(int x) {
value=x;
right=null;
left=null;
}
public BinTree getLeft() {
return left;
}
public void setLeft(int left) {
this.left = new BinTree(left);
}
public BinTree getRight() {
return right;
}
public void setRight(int right) {
this.right = new BinTree(right);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
No comments:
Post a Comment