Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 0 ms, faster than 100.00% of Java online submissions for Flip Equivalent Binary Trees.
- // Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for Flip Equivalent Binary Trees.
- class Solution {
- public boolean flipEquiv(TreeNode root1, TreeNode root2) {
- if (root1 == null || root2 == null) {
- return root1 == root2;
- }
- boolean equiv = root1.val == root2.val;
- if (!equiv) {
- return false;
- }
- boolean le = flipEquiv(root1.left, root2.left);
- boolean re = flipEquiv(root1.right, root2.right);
- if (le && re) {
- return true;
- }
- if (flipEquiv(root1.left, root2.right)) {
- return flipEquiv(root1.right, root2.left);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement