SlideShare a Scribd company logo
1 of 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());
}

More Related Content

More from rtodd432

More from rtodd432 (20)

Explain the reasons why chloride ions (Cl-) is detrimental to corrosio.docx
Explain the reasons why chloride ions (Cl-) is detrimental to corrosio.docxExplain the reasons why chloride ions (Cl-) is detrimental to corrosio.docx
Explain the reasons why chloride ions (Cl-) is detrimental to corrosio.docx
 
Explain the purpose of a data member-SolutionAnswer Data Member It is.docx
Explain the purpose of a data member-SolutionAnswer Data Member It is.docxExplain the purpose of a data member-SolutionAnswer Data Member It is.docx
Explain the purpose of a data member-SolutionAnswer Data Member It is.docx
 
Explain the function of the following methods in LinkedList 1- addFirs.docx
Explain the function of the following methods in LinkedList 1- addFirs.docxExplain the function of the following methods in LinkedList 1- addFirs.docx
Explain the function of the following methods in LinkedList 1- addFirs.docx
 
Explain the motivation behind using a header file for a C program-Solu.docx
Explain the motivation behind using a header file for a C program-Solu.docxExplain the motivation behind using a header file for a C program-Solu.docx
Explain the motivation behind using a header file for a C program-Solu.docx
 
Explain the effect doping towards an intrinsic silicon- Give one examp.docx
Explain the effect doping towards an intrinsic silicon- Give one examp.docxExplain the effect doping towards an intrinsic silicon- Give one examp.docx
Explain the effect doping towards an intrinsic silicon- Give one examp.docx
 
Explain the difference between private and proctected members of a cla.docx
Explain the difference between private and proctected members of a cla.docxExplain the difference between private and proctected members of a cla.docx
Explain the difference between private and proctected members of a cla.docx
 
Explain how goverment deficit lead to increases in the money supply-.docx
Explain how goverment deficit lead to increases in the money supply-.docxExplain how goverment deficit lead to increases in the money supply-.docx
Explain how goverment deficit lead to increases in the money supply-.docx
 
Explain how doping Si allows you to transport charges in a detector-So.docx
Explain how doping Si allows you to transport charges in a detector-So.docxExplain how doping Si allows you to transport charges in a detector-So.docx
Explain how doping Si allows you to transport charges in a detector-So.docx
 
Explain how an --interest group-- (or --special interest group--) may.docx
Explain how an --interest group-- (or --special interest group--) may.docxExplain how an --interest group-- (or --special interest group--) may.docx
Explain how an --interest group-- (or --special interest group--) may.docx
 
Explain how stock price volatility is more likely to imply risk than e.docx
Explain how stock price volatility is more likely to imply risk than e.docxExplain how stock price volatility is more likely to imply risk than e.docx
Explain how stock price volatility is more likely to imply risk than e.docx
 
Explain the concept entropy Explain the concept entropySolutionAnswe.docx
Explain the concept entropy   Explain the concept entropySolutionAnswe.docxExplain the concept entropy   Explain the concept entropySolutionAnswe.docx
Explain the concept entropy Explain the concept entropySolutionAnswe.docx
 
Explain how would you determine a covalent bond compounds bond using L.docx
Explain how would you determine a covalent bond compounds bond using L.docxExplain how would you determine a covalent bond compounds bond using L.docx
Explain how would you determine a covalent bond compounds bond using L.docx
 
Expansive (swelling) clays exist in various areas across North America.docx
Expansive (swelling) clays exist in various areas across North America.docxExpansive (swelling) clays exist in various areas across North America.docx
Expansive (swelling) clays exist in various areas across North America.docx
 
FInd and invertible matrix U so that UR - R is in Reduced Row echelon.docx
FInd and invertible matrix U so that UR - R is in Reduced Row echelon.docxFInd and invertible matrix U so that UR - R is in Reduced Row echelon.docx
FInd and invertible matrix U so that UR - R is in Reduced Row echelon.docx
 
Financial intermediaries play a crucial role in an economic crisisthey.docx
Financial intermediaries play a crucial role in an economic crisisthey.docxFinancial intermediaries play a crucial role in an economic crisisthey.docx
Financial intermediaries play a crucial role in an economic crisisthey.docx
 
Financial Institutions Describe each of the following financial instit.docx
Financial Institutions Describe each of the following financial instit.docxFinancial Institutions Describe each of the following financial instit.docx
Financial Institutions Describe each of the following financial instit.docx
 
Financial ratio analysis is conducted by managers- equity investors- l.docx
Financial ratio analysis is conducted by managers- equity investors- l.docxFinancial ratio analysis is conducted by managers- equity investors- l.docx
Financial ratio analysis is conducted by managers- equity investors- l.docx
 
Files are logical units of information created by processes- A disk ma.docx
Files are logical units of information created by processes- A disk ma.docxFiles are logical units of information created by processes- A disk ma.docx
Files are logical units of information created by processes- A disk ma.docx
 
Fill in the blank Accurate determination of abundances of isotopes bec.docx
Fill in the blank Accurate determination of abundances of isotopes bec.docxFill in the blank Accurate determination of abundances of isotopes bec.docx
Fill in the blank Accurate determination of abundances of isotopes bec.docx
 
Exercise 16-16 On January 1- 2012- Chang Corp- had 564-000 shares of c.docx
Exercise 16-16 On January 1- 2012- Chang Corp- had 564-000 shares of c.docxExercise 16-16 On January 1- 2012- Chang Corp- had 564-000 shares of c.docx
Exercise 16-16 On January 1- 2012- Chang Corp- had 564-000 shares of c.docx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

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

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