From 0539f2dae9422bc5256508fbc1afe531fd26d155 Mon Sep 17 00:00:00 2001 From: jayanpahuja20 Date: Sun, 30 Oct 2022 17:36:06 +0530 Subject: [PATCH] Added code for Merge Two Binary Trees, Range Sum of BST and Mirror Reflection Leetcode Problems Added code for Merge Two Binary Trees, Range Sum of BST and Mirror Reflection Leetcode Problems --- leetcode/MergeTwoBinaryTrees.java | 21 +++++++++++++++++++++ leetcode/MirrorReflection.java | 28 ++++++++++++++++++++++++++++ leetcode/RangeSumBST.java | 16 ++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 leetcode/MergeTwoBinaryTrees.java create mode 100644 leetcode/MirrorReflection.java create mode 100644 leetcode/RangeSumBST.java diff --git a/leetcode/MergeTwoBinaryTrees.java b/leetcode/MergeTwoBinaryTrees.java new file mode 100644 index 0000000..cff835e --- /dev/null +++ b/leetcode/MergeTwoBinaryTrees.java @@ -0,0 +1,21 @@ +class MergeTwoBinaryTrees { + //TreeNode merge = new TreeNode(); + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + if (root1==null) return root2; + if (root2==null) return root1; + if (root1==null && root2==null) return root1; + + bfs(root1,root2); + return root1; + } + public void bfs(TreeNode root1, TreeNode root2) + { + if (root1==null || root2==null) return; + if (root2.left!=null && root1.left==null) root1.left= root2.left; + if (root2.right!=null && root1.right==null) root1.right = root2.right; + if (root1!=null && root2!=root1) root1.val = root1.val + root2.val; + + bfs(root1.left,root2.left); + bfs(root1.right,root2.right); + } +} \ No newline at end of file diff --git a/leetcode/MirrorReflection.java b/leetcode/MirrorReflection.java new file mode 100644 index 0000000..3385568 --- /dev/null +++ b/leetcode/MirrorReflection.java @@ -0,0 +1,28 @@ +class mirrorReflection { + public int mirrorReflection(int p, int q) { + if (q==0) return 0; + int lcm = (p*q)/gcd(p, q); + int m = lcm/p; + int n = lcm/q; + if(m%2==0 && n%2==1) return 0; + if(m%2==1 && n%2==1) return 1; + if(m%2==1 && n%2==0) return 2; + return 0; + } + + public int gcd(int a,int b) + { + if (a==b) return a; + if (a<=0) return b; + if (b<=0) return a; + + if (a