MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / compareTrees

Function compareTrees

cpp/Trees/binary_tree_symmetry.cpp:21–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21bool 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}

Callers 1

binaryTreeSymmetryFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected