This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |