Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Yuezhu Ma/HM2/101. Symmetric Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
101. Symmetric Tree

TEST CASE: {}
{1, 2, 2}
{1, 2, 2, 3, #, #, 3}

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isSymmetricSub(root, root);
}
private boolean isSymmetricSub(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
return isSymmetricSub(t1.left, t2.right) &&
isSymmetricSub(t1.right, t2.left);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
105. Construct Binary Tree from Preorder and Inorder Traversal

TEST CASE: {}
1
/ \
2 3
/ \ \
4 5 6
pre: (1) 2 4 5 3 6 ---> left: in中找到根1后, 左边长度为左子树:(2) 4 5 right: 同理 (3)6
in : 4 2 5 (1) 3 6 ---> left: 根左边的: 4 (2) 5 right: 根右边的 (3)6 根的确定看pre的第一位


/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeSub(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
public TreeNode buildTreeSub(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) return null;

int rootVal = preorder[preStart];
int rootIndex = 0;
for (int i = inStart; i <= inEnd; i++) { // find root index in inorder
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}

int len = rootIndex - inStart; // caculate the length of left subtree
TreeNode root = new TreeNode(rootVal); // generate root node
root.left = buildTreeSub(preorder, preStart + 1, preStart + len, inorder, inStart, rootIndex - 1); // recrusive left subtree
root.right = buildTreeSub(preorder, preStart + len + 1, preEnd, inorder, rootIndex + 1, inEnd); // recrusive right subtree
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
106. Construct Binary Tree from Postorder and Inorder Traversal

TEST CASE: {}
1
/ \
2 3
/ \ \
4 5 6
post: 4 5 2 6 3 (1) ---> left: in中找到根1后左边长度为左子树:4 5(2) right: 同理 6(3)
in : 4 2 5 (1) 3 6 ---> left: 根左边的: 4 (2) 5 right: 根右边的 (3)6 根的确定看post最后一位


public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
}

public TreeNode buildTree(int[] in, int inStart, int inEnd, int[] post, int postStart, int postEnd){
if(inStart > inEnd || postStart > postEnd){
return null;
}

int rootVal = post[postEnd];
int rootIndex = 0;
for(int i = inStart; i <= inEnd; i++){ // find root index
if(in[i] == rootVal){
rootIndex = i;
break;
}
}

int len = rootIndex - inStart;
TreeNode root = new TreeNode(rootVal); //generate new root
root.left = buildTree(in, inStart, rootIndex-1, post, postStart, postStart+len-1); // recurisive left subtree
root.right = buildTree(in, rootIndex+1, inEnd, post, postStart+len, postEnd-1); // recurisive right subtree

return root;
}
37 changes: 37 additions & 0 deletions Yuezhu Ma/HM2/108. Convert Sorted Array to Binary Search Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
108. Convert Sorted Array to Binary Search Tree

TEST CASE: []
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4, 5, 6, 7]



/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null) return null;
return sortedArrayToBSTSub(nums, 0, nums.length - 1);


}
private TreeNode sortedArrayToBSTSub(int[] nums, int start, int end) {
if (start > end) return null;
int mid = start + (end - start) / 2;
TreeNode tn = new TreeNode(nums[mid]);
tn.left = sortedArrayToBSTSub(nums, start, mid - 1);
tn.right = sortedArrayToBSTSub(nums, mid + 1, end);
return tn;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
235. Lowest Common Ancestor of a Binary Search Tree

TEST CASE: {} p = null, q = null
4
/ \
2 6
/ \ / \
1 3 5 7
p = 1, q = 7
p = 1, q = 3
p = 7, q = 6



/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val <= root.val && q.val >= root.val) return root;
if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
}
if (p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
}
44 changes: 44 additions & 0 deletions Yuezhu Ma/HM2/236. Lowest Common Ancestor of a Binary Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
236. Lowest Common Ancestor of a Binary Tree

TEST CASE: {}
{1} p = 1; q = 1
{1, 2, 3, 4, 5, 6, 7} p = 2 q = 5
{1, 2, 3, 4, 5, 6, 7} p = 2 q = 3
{1, 2, 3, 4, 5, 6, 7} p = 3 q = 8



/*
SOLUTION 1: RECURISIVE + 二治法
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (root == p || root == q) return root;

TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);

if (left != null && right != null) return root;
if (left != null) return left;
if (right != null) return right;
return null;
}
}



/*
SOLUTION 2: 假设有指针指向parent, 就可以转换成两个linkedlist merge
*/


32 changes: 32 additions & 0 deletions Yuezhu Ma/HM2/257. Binary Tree Paths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
257. Binary Tree Paths

TAST CASE: {}
{1}
{1, 2, #, 3, 4, #, #, 5}


// using DFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
ArrayList<String> ret = new ArrayList<String>();
pathDFS(root, "", ret);
return ret;
}
private void pathDFS (TreeNode root, String solution, ArrayList<String> ret) {
if (root == null) return;
if (root.left == null && root.right == null) {
ret.add (solution + root.val);
}
pathDFS(root.left, solution + root.val + "->", ret);
pathDFS(root.right, solution + root.val + "->", ret);
}
}
32 changes: 32 additions & 0 deletions Yuezhu Ma/HM2/325. Maximum Size Subarray Sum Equals K.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
325. Maximum Size Subarray Sum Equals K

TEST CASE: [], k = 0
[1, -1, 5, -2, 3], k = 0
[1, -1, 5, -2, 3], k = 3
[-2, -1, 2, 1], k = 2

// 用hashmap存第0位到第i位的和,key为和,value为index。如果key里有 sum - k 更新max
public class Solution {
public int maxSubArrayLen(int[] nums, int k) {
if (nums == null) return 0;

Map<Integer, Integer> map = new HashMap<Integer,Integer>();
int sum = 0, max = 0;

for(int i = 0; i < nums.length; i++) {
sum += nums[i];
if (sum == k) {
max = i + 1;
} else if (map.containsKey(sum - k)) {
max = Math.max(max, i - map.get(sum - k));
}
if (!map.containsKey(sum)) {
map.put (sum, i);
}
}
return max;
}
}



74 changes: 74 additions & 0 deletions Yuezhu Ma/HM2/96. Unique Binary Search Trees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
96. Unique Binary Search Trees

TEST CASE: n = 0;
n = 1;
n = 2;
n = 3;
n = 4;

/* 解题思路:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3


比如,以1为根的树有几个,完全取决于有二个元素(right subtree 2, 3)的子树有几种。同理,2为根的子

树取决于一个元素(left subtree 1 and right subtree 2)的子树有几个。以3为根的情况,则与1相同

(left subtree 1, 2)。* 以i为根节点的树,其左子树由[1, i-1]构成, 其右子树由[i+1, n]构成)*

定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目。

如果数组为空,毫无疑问,只有一种BST,即空树,Count[0] =1

如果数组仅有一个元素{1},只有一种BST,单个节点 Count[1] = 1

如果数组有两个元素{1,2}, 那么有如下两种可能 1 2
\ /
2 1

Count[2] = Count[0] * Count[1] (1为根的情况, left null, right 一个点2)
+ Count[1] * Count[0] (2为根的情况, right 一个点1, right null)

再看一遍三个元素的数组,可以发现BST的取值方式如下:

Count[3] = Count[0]*Count[2] (1为根的情况, left null, right 两个点 2,3)
+ Count[1]*Count[1] (2为根的情况, left 一个点 1 right 一个点 2)
+ Count[2]*Count[0] (3为根的情况, left 两个点 1,2 righ null)

得出Count的递推公式为Count[i] = ∑ Count[0...k] * [ k+1....i] 0<=k<i-1

原始的递推式是: Ct+1 += Ci*Ct-i (0<= i <=t)

现在令变量num=t+1,那么t=num-1

所以原始递推式做变量替换得:Cnum += Ci*Cnum-1-i (0<= i <=num-1)

而num的取值范围是[1, n]因为C0已知。 */


public class Solution {
public int numTrees(int n) {
if (n == 0 || n == 1) return 1;
int[] c = new int[n + 1];
c[0] = 1;

//递推式是Ct+1 += Ci*Ct-i(0<= i <= t)
//令num = t+1
//则 t = num-1;
//因此递推式化为:
//Cnum += Ci*Cnum-1-i(0<=i<=num-1, 1<=num<=n)
//C0 = 1

for (int num = 1; num < n + 1; num++) {
for (int i = 0; i < num; i++) {
c[num] += c[i] * c[(num - 1) - i];
}
}
return c[n];
}
}

Loading