| 19 | } |
| 20 | |
| 21 | bool compareTrees(TreeNode* node1, TreeNode* node2) { |
| 22 | // Base case: if both nodes are null, they're symmetric. |
| 23 | if (!node1 && !node2) |
| 24 | return true; |
| 25 | // If one node is null and the other isn't, they aren't symmetric. |
| 26 | if (!node1 || !node2) |
| 27 | return false; |
| 28 | // If the values of the current nodes don't match, trees aren't symmetric. |
| 29 | if (node1->val != node2->val) |
| 30 | return false; |
| 31 | // Compare the 'node1's left subtree with 'node2's right subtree. If these |
| 32 | // aren't symmetric, the whole tree is not symmetric. |
| 33 | if (!compareTrees(node1->left, node2->right)) |
| 34 | return false; |
| 35 | // Compare the 'node1's right subtree with 'node2's left subtree. |
| 36 | return compareTrees(node1->right, node2->left); |
| 37 | } |
no outgoing calls
no test coverage detected