Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

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

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité

Consultez-les par la suite

1 sur 1 Publicité

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

Télécharger pour lire hors ligne

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());
}
.

Publicité
Publicité

Plus De Contenu Connexe

Plus par rtodd432 (20)

Plus récents (20)

Publicité

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()); }

×