Skip to content

Commit 3b0ff98

Browse files
committed
binary search tree
1 parent c470d29 commit 3b0ff98

File tree

10 files changed

+355
-0
lines changed

10 files changed

+355
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## practice in leetcode
2+
https://leetcode.cn/studyplan/top-100-liked/
3+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.leetcode;
2+
3+
import com.leetcode.util.ListNode;
4+
import com.leetcode.util.Utils;
5+
import java.util.ArrayList;
6+
7+
8+
/**
9+
* merge two list
10+
*/
11+
public class Practice10 {
12+
13+
/* public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
14+
ListNode newHead, currentNode;
15+
ListNode currentHead1 = head1, currentHead2 = head2;
16+
if (head1.val < head2.val) {
17+
newHead = head1;
18+
currentNode = newHead;
19+
currentHead1 = head1.next;
20+
21+
} else if (head1.val > head2.val) {
22+
newHead = head2;
23+
currentNode = newHead;
24+
currentHead2 = head2.next;
25+
} else {
26+
currentHead1 = head1.next;
27+
currentHead2 = head2.next;
28+
newHead = head1;
29+
newHead.next = head2;
30+
currentNode = newHead.next;
31+
32+
}
33+
while (currentHead1 != null && currentHead2 != null) {
34+
if (currentHead1.val < currentHead2.val || currentHead2 == null) {
35+
currentNode.next = currentHead1;
36+
currentHead1 = currentHead1.next;
37+
38+
} else if (currentHead1.val > currentHead2.val || currentHead1 == null) {
39+
currentNode.next = currentHead2;
40+
currentHead2 = currentHead2.next;
41+
} else {
42+
currentHead1 = currentHead1.next;
43+
currentHead2 = currentHead2.next;
44+
currentNode.next = currentHead1;
45+
currentNode.next.next = currentHead2;
46+
47+
}
48+
}
49+
50+
return newHead;
51+
}*/
52+
53+
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
54+
ArrayList<Integer> list = new ArrayList<Integer>();
55+
while(head1 != null && head2 != null){
56+
if(head2 == null || head1.val < head2.val){
57+
list.add(head1.val);
58+
head1 = head1.next;
59+
}else if( head1 == null || head1.val > head2.val){
60+
list.add(head2.val);
61+
head2 = head2.next;
62+
}else{
63+
list.add(head1.val);
64+
list.add(head2.val);
65+
head1 = head1.next;
66+
head2 = head2.next;
67+
}
68+
}
69+
70+
71+
ListNode head = Utils.buildList(list.stream().mapToInt(Integer::intValue).toArray());
72+
return head;
73+
}
74+
75+
76+
public static void main(String[] args) {
77+
int[] arr1 = {1, 2, 4}, arr2 = {1, 3, 4};
78+
ListNode head1 = Utils.buildList(arr1);
79+
ListNode head2 = Utils.buildList(arr2);
80+
ListNode newHead = new Practice10().mergeTwoLists(head1, head2);
81+
while (newHead != null) {
82+
System.out.println(newHead.val);
83+
newHead = newHead.next;
84+
85+
}
86+
}
87+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Practice10_MergeSortedLists {
7+
public static List<Integer> mergeSortedLists(List<Integer> list1, List<Integer> list2) {
8+
List<Integer> mergedList = new ArrayList<>();
9+
10+
// Indices for iterating through both lists
11+
int i = 0;
12+
int j = 0;
13+
14+
// Iterate through both lists and compare elements
15+
while (i < list1.size() && j < list2.size()) {
16+
if (list1.get(i) <= list2.get(j)) {
17+
mergedList.add(list1.get(i));
18+
i++;
19+
} else {
20+
mergedList.add(list2.get(j));
21+
j++;
22+
}
23+
}
24+
25+
// Add remaining elements from list1, if any
26+
while (i < list1.size()) {
27+
mergedList.add(list1.get(i));
28+
i++;
29+
}
30+
31+
// Add remaining elements from list2, if any
32+
while (j < list2.size()) {
33+
mergedList.add(list2.get(j));
34+
j++;
35+
}
36+
37+
return mergedList;
38+
}
39+
40+
public static void main(String[] args) {
41+
List<Integer> list1 = List.of(1, 3, 5, 7, 9);
42+
List<Integer> list2 = List.of(2, 4, 6, 8, 10);
43+
44+
List<Integer> mergedList = mergeSortedLists(list1, list2);
45+
System.out.println("Merged List: " + mergedList);
46+
}
47+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.leetcode;
2+
3+
public class Practice11 {
4+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.leetcode;
2+
3+
import com.leetcode.util.ListNode;
4+
import com.leetcode.util.Utils;
5+
6+
/**
7+
* intersect list
8+
*/
9+
public class Practice9 {
10+
11+
12+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
13+
ListNode currentNodeA = null;
14+
ListNode currentNodeB = null;
15+
for (int i = 1; i <= this.skipA; i++) {
16+
if(i == 1){
17+
currentNodeA = headA.next;
18+
}else{
19+
currentNodeA = currentNodeA.next;
20+
}
21+
22+
}
23+
for (int i = 1; i <= this.skipB; i++) {
24+
if(i == 1){
25+
currentNodeB = headB.next;
26+
}else{
27+
currentNodeB = currentNodeB.next;
28+
}
29+
}
30+
if (currentNodeA != null && currentNodeB != null && currentNodeA.val == currentNodeB.val) {
31+
return currentNodeA;
32+
} else {
33+
return null;
34+
}
35+
36+
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
int[] listA = {4, 1, 8, 4, 5};
42+
int[] listB = {5, 6, 1, 8, 4, 5};
43+
44+
// expect : int intersectVal = 8;
45+
Practice9 practice9 = new Practice9();
46+
practice9.listAHead = Utils.buildList(listA);
47+
practice9.listBHead = Utils.buildList(listB );
48+
ListNode intersectionNode = practice9.getIntersectionNode(practice9.listAHead, practice9.listBHead);
49+
if (intersectionNode != null) {
50+
System.out.println(intersectionNode.val);
51+
} else {
52+
System.out.println("null");
53+
}
54+
}
55+
56+
private ListNode listAHead;
57+
private ListNode listBHead;
58+
59+
private int skipA = 2;
60+
private int skipB = 3;
61+
62+
63+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.leetcode.tree;
2+
3+
4+
import com.leetcode.util.TreeNode;
5+
import java.util.List;
6+
/*
7+
* binary tree , inorder-traversal
8+
*/
9+
10+
11+
public class Practice1 {
12+
13+
// public List<Integer> inorderTraversal(TreeNode root) {
14+
//
15+
// }
16+
17+
18+
public static void main(String[] args) {
19+
20+
21+
// new Practice1().inorderTraversal();
22+
23+
24+
}
25+
26+
27+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.leetcode.util;
2+
3+
4+
/**
5+
* Binary search tree
6+
*
7+
* `recursion` algorithm
8+
*/
9+
10+
public class BinarySearchTree {
11+
12+
13+
public static TreeNode buildBST(int[] arr) {
14+
TreeNode root = new TreeNode(arr[0]);
15+
for (int ele : arr) {
16+
compareAndAdd(root, new TreeNode(ele));
17+
}
18+
return root;
19+
}
20+
21+
private static void compareAndAdd(TreeNode root, TreeNode node) {
22+
if (node.val > root.val) {
23+
if (root.right == null) {
24+
root.right = node;
25+
} else {
26+
compareAndAdd(root.right, node);
27+
}
28+
} else if (node.val < root.val) {
29+
if (root.left == null) {
30+
root.left = node;
31+
} else {
32+
compareAndAdd(root.left, node);
33+
}
34+
} else {
35+
System.out.println("have the same one , drop " + node.val);
36+
}
37+
}
38+
39+
40+
public static void main(String[] args) {
41+
int[] arr = new int[]{4, 2, 3, 1, 5, 7, 6,9,8};
42+
TreeNode root = buildBST(arr);
43+
inorderTraversal(root);
44+
45+
}
46+
47+
private static void inorderTraversal(TreeNode root) {
48+
if (root.left != null) {
49+
inorderTraversal(root.left);
50+
}
51+
System.out.println(root.val);
52+
if (root.right != null) {
53+
inorderTraversal(root.right);
54+
}
55+
56+
57+
}
58+
59+
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.leetcode.util;
2+
3+
4+
public class ListNode {
5+
public int val;
6+
public ListNode next;
7+
8+
public ListNode(int x) {
9+
val = x;
10+
next = null;
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.leetcode.util;
2+
3+
/**
4+
* Definition for a binary tree node.
5+
*/
6+
7+
public class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
12+
public TreeNode() {
13+
}
14+
15+
public TreeNode(int val) {
16+
this.val = val;
17+
}
18+
19+
public TreeNode(int val, TreeNode left, TreeNode right) {
20+
this.val = val;
21+
this.left = left;
22+
this.right = right;
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leetcode.util;
2+
3+
import com.leetcode.Practice9;
4+
5+
public class Utils {
6+
7+
/**
8+
*
9+
* @param arr
10+
* @return linked list's head node
11+
*/
12+
public static ListNode buildList(int[] arr ) {
13+
ListNode next = null;
14+
ListNode head = null;
15+
for(int i =0; i< arr.length-1; i++){
16+
if(i == 0) {
17+
head = new ListNode(arr[i]);
18+
next = new ListNode(arr[i+1]);
19+
head.next = next;
20+
}else{
21+
ListNode current = next;
22+
current.next = new ListNode(arr[i+1]);
23+
next = current.next;
24+
}
25+
}
26+
return head;
27+
}
28+
}

0 commit comments

Comments
 (0)