Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd6cf3c

Browse files
authored
Merge pull request #14 from Morbo-ui/main
Added Java 98-Validate-Binary-Search-Tree and Java 21-Merge-Two-Sorted-Lists
2 parents 897c9d0 + 07f0ea9 commit bd6cf3c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

java/21-Merge-Two-Sorted-Lists.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
final ListNode root = new ListNode();
14+
ListNode prev = root;
15+
while (list1 != null && list2 != null) {
16+
if(list1.val < list2.val) {
17+
18+
19+
prev.next = list1;
20+
list1 = list1.next;
21+
} else {
22+
prev.next = list2;
23+
list2 = list2.next;
24+
}
25+
prev = prev.next;
26+
}
27+
prev.next = list1 != null ? list1 : list2;
28+
return root.next;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public boolean isValidBST(TreeNode root) {
18+
if (root == null) return true;
19+
return dfs(root, null, null);
20+
}
21+
22+
private boolean dfs(TreeNode root, Integer min, Integer max) {
23+
if (root == null) return true;
24+
25+
if ((min != null && root.val <= min) || max != null && root.val >= max) {
26+
return false;
27+
}
28+
29+
return dfs(root.left, min, root.val) && dfs(root.right, root.val, max);
30+
31+
}
32+
}
33+

0 commit comments

Comments
 (0)