class Teste {
public static void main
(String[] args
) { ArvoreBinaria ab = new ArvoreBinaria();
ab.inserir(5);
ab.inserir(10);
ab.inserir(15);
ab.inserir(2);
ab.inserir(4);
ab.inserir(6);
ab.inserir(8);
ab.inserir(20);
System.
out.
println(ab.
soma()); }
}
class ArvoreBinaria {
private No raiz;
public void inserir(int valor) {
if (raiz == null) {
raiz = new No(valor);
} else {
No novo = new No(valor);
inserir(raiz, novo);
}
}
private void inserir(No arvore, No novo) {
if (novo.valor > arvore.valor) {
if (arvore.direito == null) {
arvore.direito = novo;
} else {
inserir(arvore.direito, novo);
}
} else {
if (arvore.esquerdo == null) {
arvore.esquerdo = novo;
} else {
inserir(arvore.esquerdo, novo);
}
}
}
public int soma() {
return raiz == null ? 0 : raiz.soma();
}
@Override
return raiz == null ? "*" : raiz.toString();
}
private static class No {
private int valor;
private No direito;
private No esquerdo;
public No(int valor) {
this.valor = valor;
}
public int soma() {
return valor
+ (direito == null ? 0 : direito.soma())
+ (esquerdo == null ? 0 : esquerdo.soma());
}
@Override
return (esquerdo == null ? "*" : "(" + esquerdo + ")")
+ valor
+ (direito == null ? "*" : "(" + direito + ")");
}
}
}
Y2xhc3MgVGVzdGUgewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIEFydm9yZUJpbmFyaWEgYWIgPSBuZXcgQXJ2b3JlQmluYXJpYSgpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihhYik7CiAgICAgICAgYWIuaW5zZXJpcig1KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYWIpOwogICAgICAgIGFiLmluc2VyaXIoMTApOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihhYik7CiAgICAgICAgYWIuaW5zZXJpcigxNSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGFiKTsKICAgICAgICBhYi5pbnNlcmlyKDIpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihhYik7CiAgICAgICAgYWIuaW5zZXJpcig0KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYWIpOwogICAgICAgIGFiLmluc2VyaXIoNik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGFiKTsKICAgICAgICBhYi5pbnNlcmlyKDgpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihhYik7CiAgICAgICAgYWIuaW5zZXJpcigyMCk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGFiKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYWIuc29tYSgpKTsKICAgIH0KfQoKY2xhc3MgQXJ2b3JlQmluYXJpYSB7CgogICAgcHJpdmF0ZSBObyByYWl6OwoKICAgIHB1YmxpYyB2b2lkIGluc2VyaXIoaW50IHZhbG9yKSB7CiAgICAgICAgaWYgKHJhaXogPT0gbnVsbCkgewogICAgICAgICAgICByYWl6ID0gbmV3IE5vKHZhbG9yKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBObyBub3ZvID0gbmV3IE5vKHZhbG9yKTsKICAgICAgICAgICAgaW5zZXJpcihyYWl6LCBub3ZvKTsKICAgICAgICB9CiAgICB9CgogICAgcHJpdmF0ZSB2b2lkIGluc2VyaXIoTm8gYXJ2b3JlLCBObyBub3ZvKSB7CiAgICAgICAgaWYgKG5vdm8udmFsb3IgPiBhcnZvcmUudmFsb3IpIHsKICAgICAgICAgICAgaWYgKGFydm9yZS5kaXJlaXRvID09IG51bGwpIHsKICAgICAgICAgICAgICAgIGFydm9yZS5kaXJlaXRvID0gbm92bzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGluc2VyaXIoYXJ2b3JlLmRpcmVpdG8sIG5vdm8pOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaWYgKGFydm9yZS5lc3F1ZXJkbyA9PSBudWxsKSB7CiAgICAgICAgICAgICAgICBhcnZvcmUuZXNxdWVyZG8gPSBub3ZvOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgaW5zZXJpcihhcnZvcmUuZXNxdWVyZG8sIG5vdm8pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyBpbnQgc29tYSgpIHsKICAgICAgICByZXR1cm4gcmFpeiA9PSBudWxsID8gMCA6IHJhaXouc29tYSgpOwogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICByZXR1cm4gcmFpeiA9PSBudWxsID8gIioiIDogcmFpei50b1N0cmluZygpOwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGNsYXNzIE5vIHsKCiAgICAgICAgcHJpdmF0ZSBpbnQgdmFsb3I7CiAgICAgICAgcHJpdmF0ZSBObyBkaXJlaXRvOwogICAgICAgIHByaXZhdGUgTm8gZXNxdWVyZG87CgogICAgICAgIHB1YmxpYyBObyhpbnQgdmFsb3IpIHsKICAgICAgICAgICAgdGhpcy52YWxvciA9IHZhbG9yOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIGludCBzb21hKCkgewogICAgICAgICAgICByZXR1cm4gdmFsb3IKICAgICAgICAgICAgICAgICAgICArIChkaXJlaXRvID09IG51bGwgPyAwIDogZGlyZWl0by5zb21hKCkpCiAgICAgICAgICAgICAgICAgICAgKyAoZXNxdWVyZG8gPT0gbnVsbCA/IDAgOiBlc3F1ZXJkby5zb21hKCkpOwogICAgICAgIH0KCiAgICAgICAgQE92ZXJyaWRlCiAgICAgICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICAgICAgcmV0dXJuIChlc3F1ZXJkbyA9PSBudWxsID8gIioiIDogIigiICsgZXNxdWVyZG8gKyAiKSIpCiAgICAgICAgICAgICAgICAgICAgKyB2YWxvcgogICAgICAgICAgICAgICAgICAgICsgKGRpcmVpdG8gPT0gbnVsbCA/ICIqIiA6ICIoIiArIGRpcmVpdG8gKyAiKSIpOwogICAgICAgIH0KICAgIH0KfQ==