Skip to content

Commit 7b90b3d

Browse files
committed
Ipsy First round Questions & Solutions Java
1 parent 9520504 commit 7b90b3d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Q1. Check if the given tree is a balanced binary tree
2+
3+
public boolean isBalanced(TreeNode root) {
4+
if(root == null)
5+
return true;
6+
int left = maxDepth(root.left);
7+
int right = maxDepth(root.right);
8+
if(Math.abs(left-right)>1)
9+
return false;
10+
return isBalanced(root.left) & isBalanced(root.right);
11+
}
12+
13+
public int maxDepth(TreeNode root){
14+
if(root == null) return 0;
15+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
16+
}
17+
18+
19+
Q2. Merge two given sorted Linked List
20+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
21+
ListNode head = new ListNode(0);
22+
ListNode p = head;
23+
24+
while(l1!=null||l2!=null){
25+
if(l1!=null&&l2!=null){
26+
if(l1.val < l2.val){
27+
p.next = l1;
28+
l1=l1.next;
29+
}else{
30+
p.next=l2;
31+
l2=l2.next;
32+
}
33+
p = p.next;
34+
}else if(l1==null){
35+
p.next = l2;
36+
break;
37+
}else if(l2==null){
38+
p.next = l1;
39+
break;
40+
}
41+
}
42+
43+
return head.next;
44+
}

0 commit comments

Comments
 (0)