Skip to content

Commit

Permalink
Added code for Merge Two Binary Trees, Range Sum of BST and Mirror Re…
Browse files Browse the repository at this point in the history
…flection Leetcode Problems

Added code for Merge Two Binary Trees, Range Sum of BST and Mirror Reflection Leetcode Problems
  • Loading branch information
jayanpahuja20 committed Oct 30, 2022
1 parent 34e0be0 commit 0539f2d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions leetcode/MergeTwoBinaryTrees.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions leetcode/MirrorReflection.java
Original file line number Diff line number Diff line change
@@ -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<b)
{
return gcd(b-a,a);
}
else
{
return gcd(a-b,b);
}
}
}
16 changes: 16 additions & 0 deletions leetcode/RangeSumBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class RangeSumBST {
int sum=0;
public int rangeSumBST(TreeNode root, int low, int high) {
bfs(root,low,high);
return sum;
}

public void bfs(TreeNode root,int low, int high)
{
if (root==null) return;
if (low<=root.val && root.val<=high) sum+=root.val;

bfs(root.left,low,high);
bfs(root.right,low,high);
}
}

0 comments on commit 0539f2d

Please sign in to comment.