Skip to content

[bky373] Solve 7th week problems #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions binary-tree-level-order-traversal/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* time: O(N)
* space: O(N)
*/
class Solution {

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (root == null) {
return result;
}

int level = 0;
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.add(root);

while (!que.isEmpty()) {
result.add(new ArrayList<Integer>());

int len = que.size();
for (int i = 0; i < len; i++) {
TreeNode cur = que.poll();
result.get(level)
.add(cur.val);

if (cur.left != null) {
que.add(cur.left);
}
if (cur.right != null) {
que.add(cur.right);
}
}
level++;
}
return result;
}
}
20 changes: 20 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* time: O(N)
* space: O(N)
*/
class Solution {

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int curVal = root.val;
int pVal = p.val;
int qVal = q.val;

if (pVal > curVal && qVal > curVal) {
return lowestCommonAncestor(root.right, p, q);
} else if (pVal < curVal && qVal < curVal) {
return lowestCommonAncestor(root.left, p, q);
} else {
return root;
}
}
}
28 changes: 28 additions & 0 deletions remove-nth-node-from-end-of-list/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* time: O(N)
* space: O(1)
*/
class Solution {

public ListNode removeNthFromEnd(ListNode head, int n) {
int sz = 0;
ListNode current = head;

while (current != null) {
current = current.next;
sz++;
}

if (sz == n) {
return head.next;
}

int removedIndex = sz - n;
current = head;
for (int i = 0; i < removedIndex-1; i++) {
current = current.next;
}
current.next = current.next.next;
return head;
}
}
33 changes: 33 additions & 0 deletions reorder-list/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* time: O(N)
* space: O(N)
*/
class Solution {

public void reorderList(ListNode head) {
if (head.next == null) {
return;
}
ListNode reorderHead = head;
List<ListNode> nodes = new ArrayList<>();
while (head != null) {
ListNode next = head.next;
head.next = null;
nodes.add(head);
head = next;
}

int left = 0;
int right = nodes.size() - 1;
while (left < right) {
reorderHead.next = nodes.get(right);
right--;
left++;
if (left <= right && left < nodes.size()) {
reorderHead = reorderHead.next;
reorderHead.next = nodes.get(left);
reorderHead = reorderHead.next;
}
}
}
}
23 changes: 23 additions & 0 deletions validate-binary-search-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* time: O(N)
* space: O(N)
*/
class Solution {

public boolean isValidBST(TreeNode root) {
return dfs(root, null, null);
}

public boolean dfs(TreeNode cur, Integer low, Integer high) {
if (cur == null) {
return true;
}
if (low != null && cur.val <= low) {
return false;
}
if (high != null && cur.val >= high) {
return false;
}
return dfs(cur.left, low, cur.val) && dfs(cur.right, cur.val, high);
}
}