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
35 changes: 35 additions & 0 deletions GuangliangLi/HW1/MergeTwoSortedLists21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class MergeTwoSortedLists21 {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = new ListNode(0);
dummyHead.next = p;

while(l1 != null && l2 !=null){
if(l1.val < l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
}

if(l1 != null){
p.next = l1;
}
if(l2 != null){
p.next = l2;
}
return dummyHead.next.next;

}
}
27 changes: 27 additions & 0 deletions GuangliangLi/HW1/RemoveDuplicates82.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class RemoveDuplicates82 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return head;
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
while(p.next != null && p.next.next != null){
if (p.next.val == p.next.next.val){
int dup = p.next.val;
while (p.next != null && p.next.val == dup){
p.next = p.next.next;
}
}else{
p = p.next;
}
}
return dummyHead.next;
}
}
36 changes: 36 additions & 0 deletions GuangliangLi/HW1/ReverseLinkedList92.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tpmp;

public class ReverseLinkedList92 {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = new ListNode(0);
p = head;
int length = 0;
while(p != null){
length++;
p = p.next;
}
if (length <= 1) return head;
ListNode preNode = new ListNode(0);
preNode = dummyHead;
ListNode curNode = new ListNode(0);
ListNode nextNode = new ListNode(0);
ListNode mNode = new ListNode(0);
for(int i = 1; i <= n; i++){
if(i < m){
preNode = preNode.next;
}else if(i == m){
mNode = preNode.next;
curNode = mNode.next;
}else if(i>m){
nextNode = curNode.next;
curNode.next = preNode.next;
preNode.next = curNode;
mNode.next = nextNode;
curNode = nextNode;
}
}
return dummyHead.next;
}
}
47 changes: 47 additions & 0 deletions GuangliangLi/HW2/BinaryTreePaths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tpmp;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTreePaths {

public List<String> getBinaryTreePaths(TreeNode root) {
if (root == null) return null;
List<String> path = new ArrayList<String>();
List<String> res = new ArrayList<String>();
getPath(root, path, res);
Stack stack = new Stack();
stack.clone();
return res;

}

public void getPath(TreeNode root, List<String> path, List<String> res){
if(path.size() == 0){
path.add(String.valueOf(root.val));
}else{
path.add("->".concat(String.valueOf(root.val)));
}
if(root.left == null && root.right == null){
connectNode(path, res);
path.remove(path.size()-1);
}else{
if(root.left != null){
getPath(root.left, path, res);
}
if(root.right != null){
getPath(root.right, path, res);
}
path.remove(path.size()-1);
}
}
public void connectNode(List<String> path, List<String> res){
StringBuffer tmp = new StringBuffer();
for(int i =0; i < path.size(); i++){
tmp.append(path.get(i));
}
res.add(tmp.toString());
}

}
48 changes: 48 additions & 0 deletions GuangliangLi/HW2/ConstructBinaryTreeFromInPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tpmp;

public class ConstructBinaryTreeFromInPost {

public TreeNode buildTree(int[] inorder, int[] postorder){
if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0) {
return null;
}
TreeNode root = new TreeNode(postorder[postorder.length-1]);
int position = getPosition(postorder[postorder.length-1], inorder);
int[] leftInOrder = getSubOrder(inorder, position, true, true);
int[] rightInOrder = getSubOrder(inorder, position, false, true);
int[] leftPostOrder = getSubOrder(postorder, position, true, false);
int[] rightPostOrder = getSubOrder(postorder, position, false, false);
root.left = buildTree(leftInOrder, leftPostOrder);
root.right = buildTree(rightInOrder, rightPostOrder);
return root;
}

private int getPosition(int target, int[] targetArr){
for(int i = 0; i < targetArr.length; i++){
if(target == targetArr[i]){
return i;
}
}
return 0;
}

private int[] getSubOrder(int[] orderArr, int position, boolean LeftFlag, boolean InFlag){
int[] retArr;
if(LeftFlag){
retArr = new int[position];
System.arraycopy(orderArr, 0, retArr, 0, position);
}else{
if(InFlag){
retArr = new int[orderArr.length - position - 1];
System.arraycopy(orderArr, position + 1, retArr, 0, orderArr.length - position - 1);
}else{
retArr = new int[orderArr.length - position - 1];
System.arraycopy(orderArr, position, retArr, 0, orderArr.length - position - 1);
}
}
return retArr;
}



}
51 changes: 51 additions & 0 deletions GuangliangLi/HW2/ConstructBinaryTreeFromInPre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tpmp;

public class ConstructBinaryTreeFromInPre {

public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null)
return null;
if (preorder.length == 0)
return null;
TreeNode root = new TreeNode(preorder[0]);
int position = getPosition(preorder[0], inorder);
int[] leftSubInOrder = getSubOrder(inorder, position, true, true);
int[] rightSubInOrder = getSubOrder(inorder, position, false, true);
int[] leftSubPreOrder = getSubOrder(preorder, position, true, false);
int[] rightSubPreOrder = getSubOrder(preorder, position, false, false);

root.left = buildTree(leftSubPreOrder, leftSubInOrder);
root.right = buildTree(rightSubPreOrder, rightSubInOrder);
return root;

}

private int getPosition(int target, int[] targetOrder) {
int i;
for (i = 0; i < targetOrder.length; i++) {
if (targetOrder[i] == target) {
return i;
}
}
return 0;
}

private int[] getSubOrder(int[] targetOrder, int pos, boolean leftFlag, boolean inFlag) {
int[] subOrder;
if (leftFlag) {
if (inFlag) {
subOrder = new int[pos];
System.arraycopy(targetOrder, 0, subOrder, 0, pos);
} else {
subOrder = new int[pos];
System.arraycopy(targetOrder, 1, subOrder, 0, pos);
}
} else {
subOrder = new int[targetOrder.length - pos - 1];
System.arraycopy(targetOrder, pos + 1, subOrder, 0, targetOrder.length - pos - 1);
}
return subOrder;

}

}
33 changes: 33 additions & 0 deletions GuangliangLi/HW2/ConvertSortedArrayToBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tpmp;

public class ConvertSortedArrayToBST {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums == null || nums.length == 0) return null;

TreeNode root = new TreeNode(nums[nums.length/2]);
if(nums.length == 1) return root;
int[] leftSubNums = getSubNums(nums, true);
int[] rightSubNums = getSubNums(nums, false);
root.left = sortedArrayToBST(leftSubNums);
root.right = sortedArrayToBST(rightSubNums);
return root;
}

private int[] getSubNums(int[] nums, boolean leftFlag){
int[] ret = new int[nums.length/2];;
if(leftFlag){
System.arraycopy(nums, 0, ret, 0, nums.length/2);
}else{
if(nums.length%2 == 0){
ret = new int[nums.length/2 -1];
System.arraycopy(nums, nums.length/2 + 1, ret, 0, nums.length/2 -1);
}else{
System.arraycopy(nums, nums.length/2 + 1, ret, 0, nums.length/2);
}

}
return ret;
}


}
21 changes: 21 additions & 0 deletions GuangliangLi/HW2/LowestCommonAncestor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tpmp;

public class LowestCommonAncestor {
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null){
return null;
}
if(root.val == p.val || root.val == q.val){
return root;
}
TreeNode leftLCA = findLowestCommonAncestor(root.left, p, q);
TreeNode rightLCA = findLowestCommonAncestor(root.right, p, q);
if(leftLCA != null && rightLCA != null){
return root;
}else if(leftLCA != null){
return leftLCA;
}else{
return rightLCA;
}
}
}
19 changes: 19 additions & 0 deletions GuangliangLi/HW2/LowestCommonAncestorBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tpmp;

public class LowestCommonAncestorBST {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val > q.val){
TreeNode r = new TreeNode(0);
r = p;
p = q;
q = r;
}
if(root.val >= p.val && root.val <= q.val){
return root;
}else if(root.val < p.val){
return lowestCommonAncestor(root.right, p, q);
}else{
return lowestCommonAncestor(root.left, p, q);
}
}
}
29 changes: 29 additions & 0 deletions GuangliangLi/HW2/MaximumDepthofBinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tpmp;

import java.util.ArrayList;

public class MaximumDepthofBinaryTree {
public int maxDepth(TreeNode root) {
ArrayList<Integer> lengthList = new ArrayList<Integer>();
int[] length = new int[1];
length[0] = 0;
getLength(root, lengthList, length);
int ret = 0;
for(int i=0; i<lengthList.size(); i++){
ret = Math.max(ret,lengthList.get(i));
}
return ret;
}

private void getLength(TreeNode root, ArrayList<Integer> lengthList, int[] length){
if(root == null) return;
length[0] = length[0] + 1;
if(root.left == null && root.right == null){
lengthList.add(length[0]);
}else{
getLength(root.left, lengthList, length);
getLength(root.right, lengthList, length);
}
length[0] = length[0] - 1;
}
}
24 changes: 24 additions & 0 deletions GuangliangLi/HW2/SymmetricTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tpmp;

public class SymmetricTree {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
if(root.left == null && root.right == null){
return true;
}
return isSymmetricTree(root.left, root.right);
}

private boolean isSymmetricTree(TreeNode left, TreeNode right){
if(left == null && right == null) return true;
if(left == null && right !=null || right == null && left != null) return false;

if(left.val == right.val){
return isSymmetricTree(left.left, right.right) && isSymmetricTree(left.right, right.left);
}else{
return false;
}
}


}
Loading