File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments