Skip to content

Instantly share code, notes, and snippets.

View cixuuz's full-sized avatar
💭
Vibe Coding

cixuuuuuuuuz cixuuz

💭
Vibe Coding
View GitHub Profile
@cixuuz
cixuuz / 257_0813.java
Last active August 14, 2017 01:01
[257. Binary Tree Paths] #leetcode
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<String>();
if (root != null) dfs(root, "", res);
return res;
}
private void dfs(TreeNode root, String path, List<String> res) {
if (root.left == null && root.right == null) res.add(path + root.val);
if (root.left != null) dfs(root.left, path+root.val+"->", res);
if (root.right != null) dfs(root.right, path+root.val+"->", res);
@cixuuz
cixuuz / 99_0813.java
Last active August 13, 2017 23:44
[99. Recover Binary Search Tree]
public class Solution {
private TreeNode firstNode = null;
private TreeNode secondNode = null;
private TreeNode preNode = new TreeNode(Integer.MIN_VALUE);
public void recoverTree(TreeNode root) {
traverse(root);
Integer tmp = firstNode.val;
firstNode.val = secondNode.val;
@cixuuz
cixuuz / 105_0812.java
Created August 12, 2017 19:59
[105. Construct Binary Tree from Preorder and Inorder Traversal] #lc_105
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return dfs(preorder, inorder, 0, 0, preorder.length);
}
private TreeNode dfs(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) {
if (inStart > inEnd || preStart >= preorder.length) return null;
TreeNode node = new TreeNode(preorder[preStart]);
@cixuuz
cixuuz / 297_0812.java
Created August 12, 2017 19:06
[297. Serialize and Deserialize Binary Tree] #lc_297
public class Codec {
private static final String SEP = ",";
private static final String NULL = "x";
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
buildString(root, sb);
return sb.toString();
}
@cixuuz
cixuuz / 112_0809.java
Created August 10, 2017 03:02
[112. Path Sum] #lc112
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if (root.left == null && root.right == null) return sum == root.val;
sum -= root.val;
boolean l = hasPathSum(root.left, sum);
boolean r = hasPathSum(root.right, sum);
sum += root.val;
return l || r;
@cixuuz
cixuuz / 222_0809.java
Created August 10, 2017 02:44
[222. Count Complete Tree Nodes] #lc0222
public class Solution {
public int countNodes(TreeNode root) {
if (root == null)
return 0;
int lh = height(root.left);
int rh = height(root.right);
if (lh == rh) return (1 << lh) + countNodes(root.right);
else return (1 << rh) + countNodes(root.left);
}
@cixuuz
cixuuz / 102_0808.java
Last active August 9, 2017 03:16
[102. Binary Tree Level Order Traversal] #leetcode #lc0102
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
Queue<TreeNode> q = new LinkedList<TreeNode>();
if (root == null) return res;
q.offer(root);
while (!q.isEmpty()) {
Integer levelLength = q.size();
@cixuuz
cixuuz / 1_0807.java
Last active August 9, 2017 02:51
[1. Two Sum] #lc0001
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
Map<Integer, Integer> visited = new HashMap<>();
for (int i=0; i<nums.length; i++) {
if (visited.containsKey(nums[i])) {
result[0] = visited.get(nums[i]);
result[1] = i;
return result;
@cixuuz
cixuuz / 101_0807.java
Created August 8, 2017 02:55
[101. Symmetric Tree] #leetcode
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return helper(root, root);
}
private boolean helper(TreeNode left, TreeNode right) {
return (left != null && right != null && left.val == right.val && helper(left.right, right.left) && helper(left.left, right.right)) || (left == null && right == null);
}
}
@cixuuz
cixuuz / 236_0806.java
Last active August 8, 2017 02:34
[236. Lowest Common Ancestor of a Binary Tree] #leetcode
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {