Skip to content

Commit 6a4d75e

Browse files
committed
Solve 7th week problems
1 parent f2970ad commit 6a4d75e

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
public List<List<Integer>> levelOrder(TreeNode root) {
8+
List<List<Integer>> result = new ArrayList<List<Integer>>();
9+
if (root == null) {
10+
return result;
11+
}
12+
13+
int level = 0;
14+
Queue<TreeNode> que = new LinkedList<TreeNode>();
15+
que.add(root);
16+
17+
while (!que.isEmpty()) {
18+
result.add(new ArrayList<Integer>());
19+
20+
int len = que.size();
21+
for (int i = 0; i < len; i++) {
22+
TreeNode cur = que.poll();
23+
result.get(level)
24+
.add(cur.val);
25+
26+
if (cur.left != null) {
27+
que.add(cur.left);
28+
}
29+
if (cur.right != null) {
30+
que.add(cur.right);
31+
}
32+
}
33+
level++;
34+
}
35+
return result;
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
8+
int curVal = root.val;
9+
int pVal = p.val;
10+
int qVal = q.val;
11+
12+
if (pVal > curVal && qVal > curVal) {
13+
return lowestCommonAncestor(root.right, p, q);
14+
} else if (pVal < curVal && qVal < curVal) {
15+
return lowestCommonAncestor(root.left, p, q);
16+
} else {
17+
return root;
18+
}
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* time: O(N)
3+
* space: O(1)
4+
*/
5+
class Solution {
6+
7+
public ListNode removeNthFromEnd(ListNode head, int n) {
8+
int sz = 0;
9+
ListNode current = head;
10+
11+
while (current != null) {
12+
current = current.next;
13+
sz++;
14+
}
15+
16+
if (sz == n) {
17+
return head.next;
18+
}
19+
20+
int removedIndex = sz - n;
21+
current = head;
22+
for (int i = 0; i < removedIndex-1; i++) {
23+
current = current.next;
24+
}
25+
current.next = current.next.next;
26+
return head;
27+
}
28+
}

reorder-list/bky373.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
public void reorderList(ListNode head) {
8+
if (head.next == null) {
9+
return;
10+
}
11+
ListNode reorderHead = head;
12+
List<ListNode> nodes = new ArrayList<>();
13+
while (head != null) {
14+
ListNode next = head.next;
15+
head.next = null;
16+
nodes.add(head);
17+
head = next;
18+
}
19+
20+
int left = 0;
21+
int right = nodes.size() - 1;
22+
while (left < right) {
23+
reorderHead.next = nodes.get(right);
24+
right--;
25+
left++;
26+
if (left <= right && left < nodes.size()) {
27+
reorderHead = reorderHead.next;
28+
reorderHead.next = nodes.get(left);
29+
reorderHead = reorderHead.next;
30+
}
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
public boolean isValidBST(TreeNode root) {
8+
return dfs(root, null, null);
9+
}
10+
11+
public boolean dfs(TreeNode cur, Integer low, Integer high) {
12+
if (cur == null) {
13+
return true;
14+
}
15+
if (low != null && cur.val <= low) {
16+
return false;
17+
}
18+
if (high != null && cur.val >= high) {
19+
return false;
20+
}
21+
return dfs(cur.left, low, cur.val) && dfs(cur.right, cur.val, high);
22+
}
23+
}

0 commit comments

Comments
 (0)