File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments