diff --git a/java/0951-flip-equivalent-binary-trees.java b/java/0951-flip-equivalent-binary-trees.java new file mode 100644 index 000000000..a32bbc095 --- /dev/null +++ b/java/0951-flip-equivalent-binary-trees.java @@ -0,0 +1,16 @@ +class Solution { + public boolean flipEquiv(TreeNode root1, TreeNode root2) { + if(root1 == null && root2 == null) return true; + if(root1 == null || root2 == null) return false; + if(root1.val != root2.val) return false; + + if(flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)){ //if they are euqal + return true; + } + if(flipEquiv(root1.right, root2.left) && flipEquiv(root1.left, root2.right)){ // if flipped once + return true; + }else{ + return false; + } + } +}