Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 114.java
Last active August 22, 2017 15:02
[114. Flatten Binary Tree to Linked List] #leetcode
class Solution {
public void flatten(TreeNode root) {
while (root != null) {
TreeNode rightNode = root.right;
root.right = root.left;
root.left = null;
// find rightmost node
TreeNode node = root;
while (node.right != null) {
node = node.right;
@cixuuz
cixuuz / 199_0822.java
Last active August 22, 2017 15:55
[199. Binary Tree Right Side View] #leetcode
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
Deque<TreeNode> dq = new LinkedList<TreeNode>();
dq.addLast(root);
while (!dq.isEmpty()) {
res.add(dq.peekLast().val);
@cixuuz
cixuuz / 230_0822.java
Last active August 22, 2017 21:47
[230. Kth Smallest Element in a BST] #leetcode
class Solution {
private Integer result;
private Integer count = 0;
public int kthSmallest(TreeNode root, int k) {
dfs(root, k);
return result;
}
public void dfs(TreeNode node, int k) {
@cixuuz
cixuuz / 94_0822.java
Created August 22, 2017 21:59
[94. Binary Tree Inorder Traversal] #leetcode
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ( !stack.isEmpty() || root != null ) {
if ( root != null ) {
stack.push(root);
root = root.left;
} else {
@cixuuz
cixuuz / 144_0822.java
Created August 22, 2017 22:35
[144. Binary Tree Preorder Traversal] #leetcode
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode curNode = root;
while ( !stack.isEmpty() || curNode != null) {
if (curNode != null) {
result.add(curNode.val);
stack.push(curNode);
@cixuuz
cixuuz / 145_0822.java
Last active August 23, 2017 01:50
[145. Binary Tree Postorder Traversal] #leetcode
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> result = new LinkedList<Integer>();
TreeNode curNode = root;
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
while ( !stack.isEmpty() || curNode != null ) {
if (curNode != null) {
stack.push(curNode);
result.addFirst(curNode.val);
@cixuuz
cixuuz / 337_0823.java
Last active August 23, 2017 19:55
[337. House Robber III] #leetcode
class Solution {
// O(n^2) O(n)
public int rob(TreeNode root) {
return dfs(root, false);
}
public int dfs(TreeNode node, Boolean robbed) {
if ( node == null ) return 0;
int rob_node = 0, not_rob_node = 0;
if (robbed) {
@cixuuz
cixuuz / 95_0823.java
Last active August 23, 2017 22:07
[95. Unique Binary Search Trees II] #leetcode
class Solution {
// O(n^2) O(n^2)
public List<TreeNode> generateTrees(int n) {
if (n == 0) return new ArrayList<TreeNode>();
return genTree(1, n);
}
private List<TreeNode> genTree(int start, int end) {
List<TreeNode> list = new ArrayList<>();