diff --git a/src/leo/class06_10/FindFirstIntersectNode.java b/src/leo/class06_10/FindFirstIntersectNode.java index c4cda3c..db0073a 100644 --- a/src/leo/class06_10/FindFirstIntersectNode.java +++ b/src/leo/class06_10/FindFirstIntersectNode.java @@ -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); } } diff --git a/src/leo/class07_10/TraversalBT.java b/src/leo/class07_10/TraversalBT.java index 8445d29..4859546 100644 --- a/src/leo/class07_10/TraversalBT.java +++ b/src/leo/class07_10/TraversalBT.java @@ -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 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 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 s1 = new Stack<>(); + Stack 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(); + + } } diff --git a/src/leo/class07_11/EncodeNaryTreeToBinaryTree.java b/src/leo/class07_11/EncodeNaryTreeToBinaryTree.java index 0b68ac3..6b7a9cc 100644 --- a/src/leo/class07_11/EncodeNaryTreeToBinaryTree.java +++ b/src/leo/class07_11/EncodeNaryTreeToBinaryTree.java @@ -171,10 +171,8 @@ public class EncodeNaryTreeToBinaryTree { head.children = de(root.left); return head; } + public static List de(TreeNode root){ - if (root == null) { - return null; - } List 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 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 de(TreeNode node) { + List 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!"); } diff --git a/src/leo/class07_11/LevelTraversalBT.java b/src/leo/class07_11/LevelTraversalBT.java index 0f7f0a5..7fe1a47 100644 --- a/src/leo/class07_11/LevelTraversalBT.java +++ b/src/leo/class07_11/LevelTraversalBT.java @@ -107,9 +107,29 @@ public class LevelTraversalBT { System.out.println(); } + public static void level4(Node head){ + if (head == null) { + return; + } + Queue 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); } diff --git a/src/leo/class07_11/PaperFolding.java b/src/leo/class07_11/PaperFolding.java index 2af66d6..cf51b09 100644 --- a/src/leo/class07_11/PaperFolding.java +++ b/src/leo/class07_11/PaperFolding.java @@ -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); } } diff --git a/src/leo/class07_11/SerializeAndReconstructTree.java b/src/leo/class07_11/SerializeAndReconstructTree.java index a81cffa..cf091b0 100644 --- a/src/leo/class07_11/SerializeAndReconstructTree.java +++ b/src/leo/class07_11/SerializeAndReconstructTree.java @@ -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 preSerial1(Node head) { + if (head == null) { + return null; + } + Queue queue = new LinkedList<>(); + preSerial1(head, queue); + return queue; + } + + public static void preSerial1(Node node, Queue 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 queue) { + if (queue == null) { + return null; + } + Node head = preDeSerialProcess1(queue); + return head; + } + + public static Node preDeSerialProcess1(Queue 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 posSerial1(Node node) { + if (node == null) { + return null; + } + Queue queue = new LinkedList<>(); + posSerial1(node, queue); + return queue; + } + + public static void posSerial1(Node node, Queue 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 queue) { + if (queue == null) { + return null; + } + Stack stack = new Stack<>(); + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + return posDeSerialProcess1(stack); + + } + + public static Node posDeSerialProcess1(Stack 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 levelSerial1(Node node) { + Queue ans = new LinkedList<>(); + if (node == null) { + ans.offer(null); + + }else{ + Node cur = node; + ans.offer(String.valueOf(cur.value)); + Queue 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 list) { + if (list == null || list.size() == 0) { + return null; + } + Node head = generateNode1(list.poll()); + Queue 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 preSerial = preSerial(head); - Queue posSerial = posSerial(head); - Node posDeSerial = posDeSerial(posSerial); - Queue levelSerial = levelSerial(head); - Node levelDeSerial = levelDeSerial(levelSerial); + Queue preSerial = preSerial1(head); + Queue posSerial = posSerial1(head); + Node posDeSerial = posDeSerial1(posSerial); + Queue 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; diff --git a/src/leo/class07_11/SuccessorNode.java b/src/leo/class07_11/SuccessorNode.java index 2a5fa71..c192670 100644 --- a/src/leo/class07_11/SuccessorNode.java +++ b/src/leo/class07_11/SuccessorNode.java @@ -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)); } diff --git a/src/leo/class07_11/TreeMaxWidth.java b/src/leo/class07_11/TreeMaxWidth.java index 10d5213..1122727 100644 --- a/src/leo/class07_11/TreeMaxWidth.java +++ b/src/leo/class07_11/TreeMaxWidth.java @@ -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 queue = new LinkedList<>(); + Map 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 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;