File tree Expand file tree Collapse file tree 1 file changed +21
-21
lines changed Expand file tree Collapse file tree 1 file changed +21
-21
lines changed Original file line number Diff line number Diff line change 1
- i /**
1
+ /**
2
2
* Definition for a binary tree node.
3
3
* public class TreeNode {
4
4
* int val;
5
5
* TreeNode left;
6
6
* TreeNode right;
7
- * TreeNode(int x) { val = x; }
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
8
14
* }
9
15
*/
10
16
class Solution {
11
- public boolean flipEquiv (TreeNode root1 , TreeNode root2 ) {
12
- if (root1 == null && root2 == null ) {
13
- return true ;
17
+ public boolean flipEquiv (TreeNode root1 , TreeNode root2 ) {
18
+ if (root1 == null && root2 == null ) {
19
+ return true ;
20
+ }
21
+ if (root1 == null || root2 == null ) {
22
+ return false ;
23
+ }
24
+ if (root1 .val != root2 .val ) {
25
+ return false ;
26
+ }
27
+ boolean noSwap = flipEquiv (root1 .left , root2 .left ) && flipEquiv (root1 .right , root2 .right );
28
+ boolean swap = flipEquiv (root1 .left , root2 .right ) && flipEquiv (root1 .right , root2 .left );
29
+ return noSwap || swap ;
14
30
}
15
- if (root1 == null || root2 == null ) {
16
- return false ;
17
- }
18
- if (root1 .val != root2 .val ) {
19
- return false ;
20
- }
21
- return (
22
- (
23
- flipEquiv (root1 .left , root2 .left ) && flipEquiv (root1 .right , root2 .right )
24
- ) ||
25
- (
26
- flipEquiv (root1 .left , root2 .right ) &&
27
- flipEquiv (root1 .right , root2 .left )
28
- )
29
- );
30
- }
31
31
}
You can’t perform that action at this time.
0 commit comments