Seu SlideShare está sendo baixado. ×

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

1 de 1 Anúncio

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

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