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 List<Integer> boundaryOfBinaryTree(TreeNode root) { | |
List<Integer> res = new ArrayList<Integer>(); | |
if (root == null) return res; | |
if (root.left == null && root.right == null) { | |
res.add(root.val); | |
return res; | |
} |
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 optimalDivision(int[] nums) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(nums[0]); | |
for (int i = 1; i < nums.length; i++) { | |
sb.append("/"); | |
if (i == 1 && nums.length>2) { | |
sb.append("("); | |
} |
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
SELECT | |
E1.id, | |
E1.month, | |
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary | |
FROM | |
(SELECT | |
id, MAX(month) AS month | |
FROM | |
Employee | |
GROUP BY id |
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 String tree2str(TreeNode t) { | |
if (t == null) return ""; | |
String left = tree2str(t.left); | |
String right = tree2str(t.right); | |
StringBuilder sb = new StringBuilder(); | |
sb.append(t.val); | |
if (right.equals("")) { | |
if (!left.equals("")) sb.append("(" + left + ")"); |
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 mergeTrees(TreeNode t1, TreeNode t2) { | |
if (t1 == null && t2 == null) return null; | |
TreeNode node = new TreeNode(t1 == null? t2.val : t2 == null? t1.val : t1.val + t2.val); | |
node.left = mergeTrees(t1 == null? null : t1.left, t2 == null? null : t2.left); | |
node.right = mergeTrees(t1 == null? null : t1.right, t2 == null? null : t2.right); | |
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(1) | |
public String solveEquation(String equation) { | |
int x = 0, c = 0, num = -1, sign = 1; | |
boolean flag = false; // if at right of equal sign | |
for (int i = 0; i < equation.length(); i++) { | |
Character s = equation.charAt(i); | |
if (Character.isDigit(s)) { | |
if (num == -1) { |
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 int widthOfBinaryTree(TreeNode root) { | |
if ( root == null ) return 0; | |
Deque<TreeNode> queue = new LinkedList<TreeNode>(); | |
List<Integer> idx = new ArrayList<Integer>(); | |
queue.addLast(root); | |
idx.add(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 { | |
private int res = 0; | |
public int sumNumbers(TreeNode root) { | |
if ( root == null ) return 0; | |
dfs(root, 0); | |
return res; | |
} | |
public void dfs(TreeNode node, int sum) { |
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[][] imageSmoother(int[][] M) { | |
if (M == null) return null; | |
int rows = M.length; | |
if (rows == 0) return new int[0][]; | |
int cols = M[0].length; | |
int result[][] = new int[rows][cols]; |
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
public class Solution { | |
// O(n) O(n) | |
public void connect(TreeLinkNode root) { | |
if ( root == null ) return; | |
Deque<TreeLinkNode> deque = new LinkedList<TreeLinkNode>(); | |
deque.addLast(root); | |
while ( !deque.isEmpty() ) { | |
int n = deque.size(); |