Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 272_0903.java
Created September 3, 2017 17:59
[272. Closest Binary Search Tree Value II] #leetcode
// O(n) O(n)
class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<Integer>();
Deque<Integer> pre = new LinkedList<Integer>();
Deque<Integer> suc = new LinkedList<Integer>();
inorder(root, target, false, pre);
inorder(root, target, true, suc);
@cixuuz
cixuuz / 270_0903.java
Last active September 3, 2017 17:12
[270. Closest Binary Search Tree Value] #leetcode
// O(log(n)) O(1)
class Solution {
public int closestValue(TreeNode root, double target) {
int a = root.val;
TreeNode kid = target < a ? root.left : root.right;
if (kid == null) return a;
int b = closestValue(kid, target);
return Math.abs(a - target) < Math.abs(b - target) ? a : b;
@cixuuz
cixuuz / 670_0902.java
Created September 3, 2017 02:42
[670. Maximum Swap] #leetcode
class Solution {
public int maximumSwap(int num) {
String temp = Integer.toString(num);
int[] nums = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) nums[i] = temp.charAt(i) - '0';
boolean flag = false;
List<Integer> maxIdx = new ArrayList<Integer>();
for (int i = 0; i < nums.length-1; i++) {
int max=0;
@cixuuz
cixuuz / 669_0902.java
Created September 3, 2017 02:11
[669. Trim a Binary Search Tree] #leetcode
class Solution {
// O(n) O(n)
public TreeNode trimBST(TreeNode root, int L, int R) {
if (root == null) return null;
if (root.val > R) return trimBST(root.left, L, R);
if (root.val < L) return trimBST(root.right, L, R);
root.left = trimBST(root.left, L, root.val);
root.right = trimBST(root.right, root.val, R);
@cixuuz
cixuuz / 671_0902.java
Created September 3, 2017 01:58
[671. Second Minimum Node In a Binary Tree] #leetcode
class Solution {
// O(n) O(h)
public int findSecondMinimumValue(TreeNode root) {
if (root == null) return -1;
return dfs(root, root.val);
}
private int dfs(TreeNode node, int largest) {
if (node == null) return -1;
if (node.val > largest) return node.val;
@cixuuz
cixuuz / 255_0902.java
Created September 3, 2017 00:04
[255. Verify Preorder Sequence in Binary Search Tree] #leetcode
class Solution {
// O(n) O(1)
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE, i = -1;
for (int p : preorder) {
if (p < low)
return false;
while (i >= 0 && p > preorder[i])
low = preorder[i--];
preorder[++i] = p;
@cixuuz
cixuuz / 156_0902.java
Created September 2, 2017 23:26
[156. Binary Tree Upside Down] #leetcode
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null) return null;
TreeNode node = root.left;
TreeNode right = root.right;
TreeNode preNode = root;
root.left = null;
root.right = null;
@cixuuz
cixuuz / 536_0902.java
Last active September 2, 2017 22:25
[536. Construct Binary Tree from String] #leetcode
class Solution {
// O(n^2) O(n)
public TreeNode str2tree(String s) {
if (s.equals("")) return null;
int i = s.indexOf("(");
if (i == -1) {
TreeNode node = new TreeNode(Integer.parseInt(s));
return node;
}
@cixuuz
cixuuz / 537_0902.java
Created September 2, 2017 21:05
[537. Complex Number Multiplication] #leetcode
class Solution {
// O(n) O(n)
public String complexNumberMultiply(String a, String b) {
int[] c = Stream.of((a+b).split("\\+|i")).mapToInt(Integer::parseInt).toArray();
return (c[0]*c[2] - c[1]*c[3]) + "+" + (c[0]*c[3] + c[1]*c[2]) + "i";
}
}
@cixuuz
cixuuz / 538_0902.java
Last active September 2, 2017 20:11
[538. Convert BST to Greater Tree] #leetcode
class Solution {
// O(n) o(1)
public TreeNode convertBST(TreeNode root) {
dfs(root, new int[1]);
return root;
}
private void dfs(TreeNode node, int[] sum) {
if (node == null) return;
dfs(node.right, sum);