Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 654.py
Created August 6, 2017 02:36
[654. Maximum Binary Tree]
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if len(nums) == 0:
return None
val = max(nums)
@cixuuz
cixuuz / 108_0806.java
Created August 7, 2017 02:45
[108. Convert Sorted Array to Binary Search 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 {
@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 {
@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 / 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 / 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 / 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 / 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 / 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 / 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]);