Skip to content

Commit 2dea3e5

Browse files
authored
Update Flip Equivalent Binary Trees.java
1 parent 975d71b commit 2dea3e5

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
i/**
1+
/**
22
* Definition for a binary tree node.
33
* public class TreeNode {
44
* int val;
55
* TreeNode left;
66
* 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+
* }
814
* }
915
*/
1016
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;
1430
}
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-
}
3131
}

0 commit comments

Comments
 (0)