pull/3/head
Leo 5 years ago
parent a97d3a5858
commit 389df193c9

@ -361,6 +361,124 @@ class FindFirstIntersectNode2{
}
}
class FindFirstIntersectNode3{
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head1.next == null || head1.next.next == null) {
return null;
}
if (head2 == null || head2.next == null || head1.next.next == null) {
return null;
}
Node loop1 = getLoop(head1);
Node loop2 = getLoop(head2);
if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
} else if (loop1 != null & loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
}
return null;
}
public static Node getLoop(Node head) {
if (head == null) {
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while (slow != fast) {
if (fast.next == null || fast.next.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
fast = head;
while (fast != slow) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head1.next == null || head1.next.next == null) {
return null;
}
if (head2 == null || head2.next == null || head2.next.next == null) {
return null;
}
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != null) {
n++;
cur1 = cur1.next;
}
while (cur2 != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1;
Node cur2;
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
cur1 = cur1.next;
n++;
}
while (cur2 != loop2) {
cur2 = cur2.next;
n--;
}
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}else{
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
}
class FindFirstIntersectNode_Main{
public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
@ -377,7 +495,7 @@ class FindFirstIntersectNode_Main{
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode3 .getIntersectNode(head1, head2).value);
// 1->2->3->4->5->6->7->4...
head1 = new Node(1);
@ -394,14 +512,14 @@ class FindFirstIntersectNode_Main{
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode3.getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6..
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode3.getIntersectNode(head1, head2).value);
}
}

@ -26,17 +26,17 @@ public class TraversalBT {
Recursive.pre1(head);
System.out.println();
UnRecursive.pre1(head);
UnRecursive.pre2(head);
System.out.println("--------");
Recursive.in1(head);
System.out.println();
UnRecursive.in1(head);
UnRecursive.in3(head);
System.out.println("---------");
Recursive.pos1(head);
System.out.println();
UnRecursive.pos1(head);
UnRecursive.pos2(head);
System.out.println("---------");
}
@ -151,6 +151,27 @@ class UnRecursive{
System.out.println();
}
public static void pre2(TreeNode head){
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = head;
stack.push(cur);
while (!stack.isEmpty()) {
cur = stack.pop();
System.out.print(cur.value + " ");
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
System.out.println();
}
/**
* :
*/
@ -215,6 +236,28 @@ class UnRecursive{
System.out.println();
}
public static void in3(TreeNode head){
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = head;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
System.out.print(cur.value + " ");
cur = cur.right;
}
}
System.out.println();
}
/**
*
*/
@ -267,6 +310,31 @@ class UnRecursive{
}
System.out.println();
}
public static void pos2(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
TreeNode cur = head;
s1.push(cur);
while (!s1.isEmpty()) {
cur = s1.pop();
s2.push(cur);
if (cur.left != null) {
s1.push(cur.left);
}
if (cur.right != null) {
s1.push(cur.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
System.out.println();
}
}

@ -171,10 +171,8 @@ public class EncodeNaryTreeToBinaryTree {
head.children = de(root.left);
return head;
}
public static List<Node> de(TreeNode root){
if (root == null) {
return null;
}
List<Node> children = new ArrayList<>();
while (root != null) {
@ -187,6 +185,52 @@ public class EncodeNaryTreeToBinaryTree {
}
}
static class Codec3{
public static TreeNode encode(Node root) {
if (root == null) {
return null;
}
TreeNode head = new TreeNode(root.val);
head.left = en(root.children);
return head;
}
public static TreeNode en(List<Node> children){
TreeNode head = null;
TreeNode cur = null;
for (Node child : children) {
TreeNode node = new TreeNode(child.val);
if (head == null) {
head = node;
}else{
cur.right = node;
}
cur = node;
cur.left = en(child.children);
}
return head;
}
public static Node decode(TreeNode root) {
if (root == null) {
return null;
}
Node head = new Node(root.val);
head.children = de(root.left);
return head;
}
public static List<Node> de(TreeNode node) {
List<Node> list = new ArrayList<>();
while (node != null) {
Node cur = new Node(node.val, de(node.left));
list.add(cur);
node = node.right;
}
return list;
}
}
static class EncodeNaryTreeToBinaryTree_Main {
public static void main(String[] args){
@ -196,10 +240,10 @@ public class EncodeNaryTreeToBinaryTree {
System.out.println("start");
for (int i = 0; i < test; i++) {
TreeNode node = generateRandomNode(maxLevel, range);
Node decodeOrigin = Codec1.decode(node);
TreeNode encodeOrigin = Codec1.encode(decodeOrigin);
Node decode = Codec1.decode(encodeOrigin);
TreeNode treeNode = Codec1.encode(decode);
Node decodeOrigin = Codec3.decode(node);
TreeNode encodeOrigin = Codec3.encode(decodeOrigin);
Node decode = Codec3.decode(encodeOrigin);
TreeNode treeNode = Codec3.encode(decode);
if (!isEqualsNode(treeNode, encodeOrigin)) {
System.out.println("fuck!");
}

@ -107,9 +107,29 @@ public class LevelTraversalBT {
System.out.println();
}
public static void level4(Node head){
if (head == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
Node cur = head;
queue.offer(cur);
while (!queue.isEmpty()) {
cur = queue.poll();
System.out.print(cur.value+" ");
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
System.out.println();
}
public static void main(String[] args){
Node node = randomTreeNode(10);
level3(node);
level4(node);
}

@ -27,9 +27,27 @@ public class PaperFolding {
processPrint(i + 1, n, false);
}
public static void printAllFolds1(int n) {
if (n < 0) {
return;
}
processPrint1(1, n, true);
System.out.println();
}
public static void processPrint1(int i, int n, boolean du) {
if (i > n) {
return;
}
processPrint1(i + 1, n, true);
System.out.print(du ? "凹 " : "凸 ");
processPrint1(i + 1, n, false);
}
public static void main(String[] args){
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
printAllFolds(4);
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
printAllFolds1(4);
}
}

@ -1,5 +1,7 @@
package leo.class07_11;
import com.sun.tools.javac.util.ListBuffer;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
@ -42,6 +44,27 @@ public class SerializeAndReconstructTree {
}
}
public static Queue<String> preSerial1(Node head) {
if (head == null) {
return null;
}
Queue<String> queue = new LinkedList<>();
preSerial1(head, queue);
return queue;
}
public static void preSerial1(Node node, Queue<String> queue) {
if (node == null) {
queue.offer(null);
}else{
queue.offer(String.valueOf(node.value));
preSerial(node.left, queue);
preSerial(node.right, queue);
}
}
/**
*
*/
@ -63,6 +86,27 @@ public class SerializeAndReconstructTree {
return head;
}
public static Node preDeSerial1(Queue<String> queue) {
if (queue == null) {
return null;
}
Node head = preDeSerialProcess1(queue);
return head;
}
public static Node preDeSerialProcess1(Queue<String> queue) {
String nodeValue = queue.poll();
if (nodeValue == null) {
return null;
}
Node head = new Node(Integer.valueOf(nodeValue));
head.left = preDeSerialProcess(queue);
head.right = preDeSerialProcess(queue);
return head;
}
/**
*
*/
@ -82,6 +126,26 @@ public class SerializeAndReconstructTree {
}
}
public static Queue<String> posSerial1(Node node) {
if (node == null) {
return null;
}
Queue<String> queue = new LinkedList<>();
posSerial1(node, queue);
return queue;
}
public static void posSerial1(Node node, Queue<String> queue) {
if (node == null) {
queue.offer(null);
}else{
posSerial1(node.left, queue);
posSerial1(node.right, queue);
queue.offer(String.valueOf(node.value));
}
}
/**
*
*/
@ -108,6 +172,30 @@ public class SerializeAndReconstructTree {
return head;
}
public static Node posDeSerial1(Queue<String> queue) {
if (queue == null) {
return null;
}
Stack<String> stack = new Stack<>();
while (!queue.isEmpty()) {
stack.push(queue.poll());
}
return posDeSerialProcess1(stack);
}
public static Node posDeSerialProcess1(Stack<String> stack) {
String v = stack.pop();
if (v == null) {
return null;
}
Node head = new Node(Integer.valueOf(v));
head.right = posDeSerialProcess1(stack);
head.left = posDeSerialProcess1(stack);
return head;
}
/**
*
*/
@ -164,6 +252,63 @@ public class SerializeAndReconstructTree {
return head;
}
public static Queue<String> levelSerial1(Node node) {
Queue<String> ans = new LinkedList<>();
if (node == null) {
ans.offer(null);
}else{
Node cur = node;
ans.offer(String.valueOf(cur.value));
Queue<Node> queue = new LinkedList<>();
queue.offer(cur);
while (!queue.isEmpty()) {
cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
ans.offer(String.valueOf(cur.left.value));
}else{
ans.offer(null);
}
if (cur.right != null) {
queue.offer(cur.right);
ans.offer(String.valueOf(cur.right.value));
}else{
ans.offer(null);
}
}
}
return ans;
}
public static Node levelDeSerial1(Queue<String> list) {
if (list == null || list.size() == 0) {
return null;
}
Node head = generateNode1(list.poll());
Queue<Node> queue = new LinkedList<>();
if (head != null) {
queue.offer(head);
}
Node cur = null;
while (!queue.isEmpty()) {
cur = queue.poll();
cur.left = generateNode1(list.poll());
cur.right = generateNode1(list.poll());
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
return head;
}
public static Node generateNode(String v) {
if (v == null) {
return null;
@ -172,6 +317,15 @@ public class SerializeAndReconstructTree {
return node;
}
public static Node generateNode1(String v) {
if (v == null) {
return null;
}
Node node = new Node(Integer.valueOf(v));
return node;
}
/**
* :
* @author Leo
@ -186,13 +340,13 @@ public class SerializeAndReconstructTree {
System.out.println("start");
for (int i = 0; i < testTime; i++) {
Node head = generateRandomNode(maxLevel, range);
Queue<String> preSerial = preSerial(head);
Queue<String> posSerial = posSerial(head);
Node posDeSerial = posDeSerial(posSerial);
Queue<String> levelSerial = levelSerial(head);
Node levelDeSerial = levelDeSerial(levelSerial);
Queue<String> preSerial = preSerial1(head);
Queue<String> posSerial = posSerial1(head);
Node posDeSerial = posDeSerial1(posSerial);
Queue<String> levelSerial = levelSerial1(head);
Node levelDeSerial = levelDeSerial1(levelSerial);
Node preDeSerial = preDeSerial(preSerial);
Node preDeSerial = preDeSerial1(preSerial);
if (!isEqualsNode(preDeSerial, head)) {
System.out.println("preDeSerial ==> fuck");
break;

@ -63,6 +63,26 @@ public class SuccessorNode {
}
}
public static Node getSuccessorNode2(Node node) {
if (node == null) {
return null;
}
if (node.right != null) {
Node cur = node.right;
while (cur.left != null) {
cur = cur.left;
}
return cur;
}else{
Node parent = node.parent;
while (parent != null && parent.right == node) {
node = parent;
parent = node.parent;
}
return parent;
}
}
public static void main(String[] args) {
Node head = new Node(6);
head.parent = null;
@ -86,25 +106,25 @@ public class SuccessorNode {
head.right.right.parent = head.right;
Node test = head.left.left;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.left.left.right;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.left;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.left.right;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.left.right.right;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.right.left.left;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.right.left;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.right;
System.out.println(test.value + " next: " + getSuccessorNode1(test).value);
System.out.println(test.value + " next: " + getSuccessorNode2(test).value);
test = head.right.right; // 10's next is null
System.out.println(test.value + " next: " + getSuccessorNode1(test));
System.out.println(test.value + " next: " + getSuccessorNode2(test));
}

@ -1,5 +1,6 @@
package leo.class07_11;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
@ -58,6 +59,42 @@ public class TreeMaxWidth {
return max;
}
public static int maxWidthUseMap1(Node head) {
if (head == null) {
return 0;
}
int max = 0;
int curLevel = 1;
int maxCurCount = 0;
Node cur;
Queue<Node> queue = new LinkedList<>();
Map<Node, Integer> map = new HashMap<>();
queue.offer(head);
map.put(head, curLevel);
while (!queue.isEmpty()) {
cur = queue.poll();
int curNodeLevel = map.get(cur);
if (cur.left != null) {
queue.offer(cur.left);
map.put(cur.left, curNodeLevel + 1);
}
if (cur.right != null) {
queue.offer(cur.right);
map.put(cur.right, curNodeLevel + 1);
}
if (curNodeLevel == curLevel) {
maxCurCount++;
}else{
max = Math.max(max, maxCurCount);
curLevel++;
maxCurCount = 1;
}
}
max = Math.max(max, maxCurCount);
return max;
}
public static int maxWidthNoMap(Node head) {
if (head == null) {
return 0;
@ -89,18 +126,49 @@ public class TreeMaxWidth {
return max;
}
public static int maxWidthNoMap1(Node head){
if (head == null) {
return 0;
}
Node curEnd = head;
Node nextEnd = null;
int max = 0;
int maxLevel = 0;
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
Node cur;
while (!queue.isEmpty()) {
cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
nextEnd = cur.left;
}
if (cur.right != null) {
queue.offer(cur.right);
nextEnd = cur.right;
}
maxLevel++;
if (cur == curEnd) {
curEnd = nextEnd;
max = Math.max(max, maxLevel);
maxLevel = 0;
}
}
return max;
}
public static void main(String[] args){
int maxSize = 10;
int range = 100;
int test = 1000;
int test = 100000;
System.out.println("start!");
for (int i = 0; i < test; i++) {
Node head = generateRandomNode(maxSize, range);
int i1 = maxWidthUseMap(head);
int i2 = maxWidthNoMap(head);
int i1 = maxWidthUseMap1(head);
int i2 = maxWidthNoMap1(head);
if (i1 != i2) {
System.out.println("i1: " + i1 + " i2: " + i2);
break;

Loading…
Cancel
Save