This commit is contained in:
fanxb 2024-03-26 23:26:24 +08:00
parent 0a779cdd32
commit 5770a9f011
7 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.fanxb.common;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class Q144 {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
res.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return res;
}
}

View File

@ -0,0 +1,25 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Q145 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<Integer> helpStack = new Stack<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
helpStack.push(node.val);
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
while (!helpStack.isEmpty()) {
res.add(helpStack.pop());
}
return res;
}
}

View File

@ -0,0 +1,20 @@
package com.fanxb.common;
public class Q230 {
private int res = 0;
private int count = 0;
public int kthSmallest(TreeNode root, int k) {
dfs(root, k);
return res;
}
private void dfs(TreeNode root, int k) {
if (root == null) return;
dfs(root.left, k);
if (++count == k) {
res = root.val;
}
dfs(root.right, k);
}
}

View File

@ -0,0 +1,45 @@
package com.fanxb.common;
import java.util.*;
public class Q380 {
private static class RandomizedSet {
private List<Integer> list;
private Map<Integer, Integer> map;
private Random random;
public RandomizedSet() {
list = new ArrayList<>();
map = new HashMap<>();
random = new Random();
}
public boolean insert(int val) {
Integer index = map.get(val);
if (index != null) return false;
list.add(val);
map.put(val, list.size() - 1);
System.out.println(list.toString());
return true;
}
public boolean remove(int val) {
Integer index = map.get(val);
if (index == null) return false;
int last = list.size() - 1;
int lastVal = list.get(last);
list.set(index, lastVal);
list.remove(last);
map.remove(val);
//避免长度为1时将删除的元素放到map
if (!list.isEmpty()) map.put(lastVal, index);
System.out.println(list.toString());
return true;
}
public int getRandom() {
int size = list.size();
return size == 1 ? list.get(0) : list.get(Math.abs(random.nextInt()) % size);
}
}
}

View File

@ -0,0 +1,21 @@
package com.fanxb.common;
public class Q530 {
private int res = Integer.MAX_VALUE;
private Integer lastVal = null;
public int getMinimumDifference(TreeNode root) {
dfs(root);
return res;
}
private void dfs(TreeNode root) {
if (root == null) return;
dfs(root.left);
if (lastVal != null) {
res = Math.min(res, Math.abs(root.val - lastVal));
}
lastVal = root.val;
dfs(root.right);
}
}

View File

@ -0,0 +1,25 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Q94 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode cur = root.left;
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode node = stack.pop();
res.add(node.val);
cur = node.right;
}
return res;
}
}

View File

@ -0,0 +1,20 @@
package com.fanxb.common;
public class Q98 {
private Integer lastVal = null;
public boolean isValidBST(TreeNode root) {
return dfs(root);
}
private boolean dfs(TreeNode root) {
if (root == null) return true;
boolean left = dfs(root.left);
if (lastVal != null && lastVal >= root.val) {
return false;
}
lastVal = root.val;
boolean right = dfs(root.right);
return left && right;
}
}