From 6df3c37ee20673432578a5b35d4d0c66542db6e7 Mon Sep 17 00:00:00 2001 From: Anukriti Banerjee <37472545+Anukriti167@users.noreply.github.com> Date: Wed, 31 May 2023 13:43:40 +0530 Subject: [PATCH] Create 0951-flip-equivalent-binary-trees.java --- java/0951-flip-equivalent-binary-trees.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 java/0951-flip-equivalent-binary-trees.java 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; + } + } +}