O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Extend the class BinaryTree to include a boolean method similarTrees t.docx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio

Confira estes a seguir

1 de 1 Anúncio

Extend the class BinaryTree to include a boolean method similarTrees t.docx

Baixar para ler offline

Extend the class BinaryTree to include a boolean method similarTrees that determines whether the shapes of two trees are the same (the nodes do not have to contain the same values, but each node must have the same number of children)
Solution
public boolean similarTrees(treeNode r1, treeNode r2) {
if (r1 == null && r2 == null){
return true;
}
if ((r1 == null && r2 != null) || (r1 != null && r2 == null)){
return false;
}
return similarTrees(r1.getLeftSon(), r2.getLeftSon()) && similarTrees(r1.getRightSon(), r2.getRightSon());
}
.

Extend the class BinaryTree to include a boolean method similarTrees that determines whether the shapes of two trees are the same (the nodes do not have to contain the same values, but each node must have the same number of children)
Solution
public boolean similarTrees(treeNode r1, treeNode r2) {
if (r1 == null && r2 == null){
return true;
}
if ((r1 == null && r2 != null) || (r1 != null && r2 == null)){
return false;
}
return similarTrees(r1.getLeftSon(), r2.getLeftSon()) && similarTrees(r1.getRightSon(), r2.getRightSon());
}
.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Mais de rtodd432 (20)

Mais recentes (20)

Anúncio

Extend the class BinaryTree to include a boolean method similarTrees t.docx

  1. 1. Extend the class BinaryTree to include a boolean method similarTrees that determines whether the shapes of two trees are the same (the nodes do not have to contain the same values, but each node must have the same number of children) Solution public boolean similarTrees(treeNode r1, treeNode r2) { if (r1 == null && r2 == null){ return true; } if ((r1 == null && r2 != null) || (r1 != null && r2 == null)){ return false; } return similarTrees(r1.getLeftSon(), r2.getLeftSon()) && similarTrees(r1.getRightSon(), r2.getRightSon()); }

×