Advertisement
K_S_

Untitled

Nov 6th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. // Runtime: 0 ms, faster than 100.00% of Java online submissions for Flip Equivalent Binary Trees.
  2. // Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for Flip Equivalent Binary Trees.
  3.  
  4. class Solution {
  5.     public boolean flipEquiv(TreeNode root1, TreeNode root2) {
  6.         if (root1 == null || root2 == null) {
  7.             return root1 == root2;
  8.         }
  9.  
  10.         boolean equiv = root1.val == root2.val;
  11.         if (!equiv) {
  12.             return false;
  13.         }
  14.  
  15.         boolean le = flipEquiv(root1.left, root2.left);
  16.         boolean re = flipEquiv(root1.right, root2.right);
  17.         if (le && re) {
  18.             return true;
  19.         }
  20.  
  21.         if (flipEquiv(root1.left, root2.right)) {
  22.             return flipEquiv(root1.right, root2.left);
  23.         }
  24.         return false;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement