Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by registered user SKYDOS ( 17 years ago )
import java.io.*;
public class Main {
public static class node {
private int key;
node left, right;
node (int k) {
key=k;
left=right=null;
}
}
static node root;
static StreamTokenizer in;
static PrintWriter out;
public static void insert (int num, node r) {
for (;;) {
if (r.key==num) break;
else if (r.key>num && r.left==null) {
r.left = new node (num); break;
} else if (r.key<num && r.right==null) {
r.right = new node (num); break;
}
if (r.key>num) {
r = r.left;
} else r = r.right;
}
}
public static void walkTree (node root) {
if (root!=null) {
walkTree (root.left);
if ((root.left!=null && root.right==null) || (root.left==null && root.right!=null))
out.print (root.key+" ");
walkTree (root.right);
}
}
public static void main (String[] args) throws IOException {
in = new StreamTokenizer (new BufferedReader(new InputStreamReader (System.in, "ISO-8859-1")));
out = new PrintWriter (new OutputStreamWriter (System.out, "ISO-8859-1"));
for (int i=0;;i++) {
in.nextToken();
if ((int)in.nval==0) break;
if (i==0) {
root = new node ((int)in.nval);
}
insert ((int)in.nval, root);
}
walkTree (root);
out.close();
}
}
Revise this Paste