Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 235_0801.java
Last active August 5, 2017 01:17
[235 Lowest Common Ancestor of a 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 / 173_0801.java
Last active August 5, 2017 01:17
[173 Binary Search Tree Iterator] #leetcode
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
@cixuuz
cixuuz / 100_08_01.java
Last active August 5, 2017 01:26
[100 Same 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 / 104_0730.java
Last active August 5, 2017 01:26
[104 Maximum Depth of Binary Tree] #leetcode
public class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
@cixuuz
cixuuz / 96_0801.java
Last active August 5, 2017 01:27
[96 Unique Binary Search Trees] #leetcode
public class Solution {
public int numTrees(int n) {
int[] dp = new int[n+1];
dp[0] = dp[1] = 1;
for (int i=2; i <= n; i++) {
for (int j=1; j <= i; j++) {
dp[i] += dp[j-1] * dp[i-j];
}
@cixuuz
cixuuz / 110_0802.java
Last active August 5, 2017 01:27
[110 Balanced 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 / 124_0803.java
Last active August 5, 2017 01:27
[124 Binary Tree Maximum Path Sum] #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 / 226_0730.py
Last active August 5, 2017 03:27
[226 Invert Binary Tree] #leetcode
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def invertTree(self, root):
"""
@cixuuz
cixuuz / 116_0803.py
Last active August 5, 2017 17:08
[116 Populating Next Right Pointers in Each Node] #leetcode
# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
from collections import deque
@cixuuz
cixuuz / 653.py
Created August 6, 2017 02:30
[653. Two Sum IV - Input is a BST] #leetcode
class Solution:
cor = set()
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
if root is None:
return False