diff --git a/5.leetcode/src/com/fanxb/common/Q144.java b/5.leetcode/src/com/fanxb/common/Q144.java new file mode 100644 index 0000000..6dba935 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q144.java @@ -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 preorderTraversal(TreeNode root) { + List res = new LinkedList<>(); + if (root == null) return res; + Stack 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q145.java b/5.leetcode/src/com/fanxb/common/Q145.java new file mode 100644 index 0000000..ede47ee --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q145.java @@ -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 postorderTraversal(TreeNode root) { + List res = new ArrayList<>(); + if (root == null) return res; + Stack helpStack = new Stack<>(); + Stack 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q230.java b/5.leetcode/src/com/fanxb/common/Q230.java new file mode 100644 index 0000000..ab2887b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q230.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q380.java b/5.leetcode/src/com/fanxb/common/Q380.java new file mode 100644 index 0000000..5a890f8 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q380.java @@ -0,0 +1,45 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q380 { + private static class RandomizedSet { + private List list; + private Map 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); + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q530.java b/5.leetcode/src/com/fanxb/common/Q530.java new file mode 100644 index 0000000..40123a9 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q530.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q94.java b/5.leetcode/src/com/fanxb/common/Q94.java new file mode 100644 index 0000000..f54a2a6 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q94.java @@ -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 inorderTraversal(TreeNode root) { + List res = new ArrayList<>(); + if (root == null) return res; + Stack 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q98.java b/5.leetcode/src/com/fanxb/common/Q98.java new file mode 100644 index 0000000..6bd73f7 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q98.java @@ -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; + } +}