0% found this document useful (0 votes)
3 views

Tree Structure are same in java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tree Structure are same in java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tree Structure

Both Tree 1 and Tree 2 have the following structure:

mathematica
Copy code
Tree 1: Tree 2:
1 1
/ \ / \
2 3 2 3
/ \ / \
4 5 4 5

Step-by-Step Comparison

1. Compare Root Nodes (1):


o Both Tree 1 and Tree 2 have the root node 1.
o The values are equal, so the function checks their children.

sql
Copy code
Tree 1: 1
\
-> Check left and right children
Tree 2: 1
\
-> Check left and right children

2. Compare Left Child of Root (2):


o The left child of the root in both trees is 2.
o The values are equal, so the function checks their children.

sql
Copy code
Tree 1: 2
\
-> Check left and right children
Tree 2: 2
\
-> Check left and right children

3. Compare Left Child of Node 2 (4):


o The left child of node 2 in both trees is 4.
o The values are equal, and since both nodes have no children (null), the function
returns true for this subtree.

yaml
Copy code
Tree 1: 4
/ \
null null
Tree 2: 4
/ \
null null

4. Compare Right Child of Node 2 (5):


o The right child of node 2 in both trees is 5.
o The values are equal, and since both nodes have no children (null), the function
returns true for this subtree.

yaml
Copy code
Tree 1: 5
/ \
null null
Tree 2: 5
/ \
null null

5. Compare Right Child of Root (3):


o The right child of the root in both trees is 3.
o The values are equal, and since both nodes have no children (null), the function
returns true for this subtree.
yaml
Copy code
Tree 1: 3
/ \
null null
Tree 2: 3
/ \
null null

Final Comparison Summary

 Root (1) == Root (1): True


o Left Child (2) == Left Child (2): True
 Left Child (4) == Left Child (4): True
 Right Child (5) == Right Child (5): True
o Right Child (3) == Right Child (3): True

Since all comparisons return true, the final output of the isSameTree function is true,
indicating that Tree 1 and Tree 2 are identical.

You might also like