Skip to content

Commit 3205a0e

Browse files
committed
Sync LeetCode submission - Symmetric Tree (c)
1 parent 5b4a6e5 commit 3205a0e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

problems/symmetric_tree/solution.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
int isMirror(struct TreeNode*, struct TreeNode*);
2+
3+
int isSymmetric(struct TreeNode* root) {
4+
return !root || isMirror(root->left, root->right);
5+
}
6+
7+
int isMirror(struct TreeNode* tree1, struct TreeNode* tree2) {
8+
// A non-existent tree is always a mirror of itself
9+
// If only one tree exists they aren't mirrors
10+
// The value of each tree must be equal to be mirrors
11+
// If the tree has a left subtree, the other tree must have a right subtree, and vice versa
12+
// Finally check if the left subtree is a mirror of the right subtree and vice versa
13+
return (tree1 == tree2) || ((tree1 && tree2) && (tree1->val == tree2->val) && !(tree1->left && !(tree2->right))
14+
&& !(tree1->right && !(tree2->left)) && isMirror(tree1->left, tree2->right) && isMirror(tree1->right, tree2->left));
15+
16+
}

0 commit comments

Comments
 (0)