diff --git a/src/leetcode/lc290_wordPattern.java b/src/leetcode/lc290_wordPattern.java new file mode 100644 index 0000000..203a0ae --- /dev/null +++ b/src/leetcode/lc290_wordPattern.java @@ -0,0 +1,44 @@ +package leetcode; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author Leo + * @ClassName lc290_wordPattern + * @DATE 2020/12/16 11:34 上午 + * @Description + * 给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。 + */ +public class lc290_wordPattern { + + public static boolean wordPattern(String pattern, String s) { + char[] p = pattern.toCharArray(); + String[] sArray = s.split(" "); + if (p.length != sArray.length) { + return false; + } + Map map = new HashMap<>(); + Set set = new HashSet<>(); + for (int i = 0;i < sArray.length; i++) { + if(!map.containsKey(p[i])) { + if (set.contains(sArray[i])){ + return false; + } + map.put(p[i],sArray[i]); + set.add(sArray[i]); + } else { + if(!map.get(p[i]).equals(sArray[i])){ + return false; + } + } + } + return true; + } + public static void main(String[] args){ + boolean b = wordPattern("abba","dog dog dog dog"); + System.out.println(b); + } +} diff --git a/src/leo/class07_11/LevelTraversalBT.java b/src/leo/class07_11/LevelTraversalBT.java index 7fe1a47..6168e48 100644 --- a/src/leo/class07_11/LevelTraversalBT.java +++ b/src/leo/class07_11/LevelTraversalBT.java @@ -127,9 +127,33 @@ public class LevelTraversalBT { System.out.println(); } + public static void level5(Node head) { + if (head == null) { + return; + } + Queue queue = new LinkedList<>(); + queue.offer(head); + Node cur = null; + Node l = null; + Node r = null; + while (!queue.isEmpty()) { + cur = queue.poll(); + l = cur.left; + r = cur.right; + if (l != null) { + queue.offer(l); + } + if (r != null) { + queue.offer(r); + } + System.out.print(cur.value+" "); + } + System.out.println(); + } + public static void main(String[] args){ Node node = randomTreeNode(10); - level4(node); + level5(node); } diff --git a/src/leo/class07_11/PaperFolding.java b/src/leo/class07_11/PaperFolding.java index cf51b09..0f709c6 100644 --- a/src/leo/class07_11/PaperFolding.java +++ b/src/leo/class07_11/PaperFolding.java @@ -44,10 +44,26 @@ public class PaperFolding { processPrint1(i + 1, n, false); } + public static void printAllFolds2(int n) { + processPrint2(1, n, true); + System.out.println(); + + } + static void processPrint2(int i,int n,boolean v) { + if (i > n) { + return; + } + processPrint(i + 1, n, true); + System.out.print(v ? "凹 " : "凸 "); + processPrint(i + 1, n, false); + + } + public static void main(String[] args){ //凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸 //凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸 //凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸 - printAllFolds1(4); + //凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸 + printAllFolds2(4); } } diff --git a/src/leo/class07_11/SerializeAndReconstructTree.java b/src/leo/class07_11/SerializeAndReconstructTree.java index cf091b0..066cb72 100644 --- a/src/leo/class07_11/SerializeAndReconstructTree.java +++ b/src/leo/class07_11/SerializeAndReconstructTree.java @@ -64,6 +64,25 @@ public class SerializeAndReconstructTree { } } + public static Queue preSerial2(Node head) { + if (head == null) { + return null; + } + Queue queue = new LinkedList<>(); + preSerial2(head, queue); + return queue; + } + + static void preSerial2(Node node, Queue queue) { + if (node == null) { + queue.offer(null); + return; + } + queue.offer(String.valueOf(node.value)); + preSerial2(node.left, queue); + preSerial2(node.right, queue); + } + /** * 先序方式反序列化 @@ -106,6 +125,25 @@ public class SerializeAndReconstructTree { return head; } + static Node preDeSerial2(Queue queue) { + if (queue == null) { + return null; + } + return preDeSerialProcess2(queue); + } + + static Node preDeSerialProcess2(Queue queue) { + String cur = queue.poll(); + + if (cur == null) { + return null; + } + Node head = new Node(Integer.valueOf(cur)); + head.left = preDeSerialProcess2(queue); + head.right = preDeSerialProcess2(queue); + return head; + + } /** * 后续方式序列化队列 @@ -146,8 +184,28 @@ public class SerializeAndReconstructTree { } + static Queue posSerial2(Node head) { + if (head == null) { + return null; + } + Queue queue = new LinkedList<>(); + posSerial2(head, queue); + return queue; + } + + static void posSerial2(Node node, Queue queue) { + if (node == null) { + queue.offer(null); + }else { + posSerial2(node.left, queue); + posSerial2(node.right, queue); + queue.offer(String.valueOf(node.value)); + } + + } /** * 后续方式反序列化 + * 左右头 */ public static Node posDeSerial(Queue queue) { if (queue.isEmpty() || queue == null) { @@ -196,6 +254,29 @@ public class SerializeAndReconstructTree { } + static Node posDeSerial2(Queue queue) { + + if (queue == null) { + return null; + } + Stack stack = new Stack<>(); + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + return posDeSerialProcess2(stack); + } + + static Node posDeSerialProcess2(Stack stack) { + String cur = stack.pop(); + if (cur == null) { + return null; + } + Node head = new Node(Integer.valueOf(cur)); + head.right = posDeSerialProcess2(stack); + head.left = posDeSerialProcess2(stack); + return head; + } + /** * 按层序列化 */ @@ -340,13 +421,13 @@ public class SerializeAndReconstructTree { System.out.println("start"); for (int i = 0; i < testTime; i++) { Node head = generateRandomNode(maxLevel, range); - Queue preSerial = preSerial1(head); - Queue posSerial = posSerial1(head); - Node posDeSerial = posDeSerial1(posSerial); + Queue preSerial = preSerial2(head); + Queue posSerial = posSerial2(head); + Node posDeSerial = posDeSerial2(posSerial); Queue levelSerial = levelSerial1(head); Node levelDeSerial = levelDeSerial1(levelSerial); - Node preDeSerial = preDeSerial1(preSerial); + Node preDeSerial = preDeSerial2(preSerial); if (!isEqualsNode(preDeSerial, head)) { System.out.println("preDeSerial ==> fuck"); break; diff --git a/src/leo/class07_11/TreeMaxWidth.java b/src/leo/class07_11/TreeMaxWidth.java index 1122727..301ff01 100644 --- a/src/leo/class07_11/TreeMaxWidth.java +++ b/src/leo/class07_11/TreeMaxWidth.java @@ -157,6 +157,36 @@ public class TreeMaxWidth { return max; } + public static int maxWidthNoMap2(Node head) { + if (head == null) { + return 0; + } + Node curEnd = head; + Node nextEnd = null; + int curLevel = 0; + int maxLevel = 0; + Queue queue = new LinkedList<>(); + queue.offer(head); + Node cur = null; + 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; + } + curLevel++; + if (cur == curEnd) { + curEnd = nextEnd; + maxLevel = Math.max(maxLevel, curLevel); + curLevel = 0; + } + } + return maxLevel; + } public static void main(String[] args){ @@ -168,7 +198,7 @@ public class TreeMaxWidth { for (int i = 0; i < test; i++) { Node head = generateRandomNode(maxSize, range); int i1 = maxWidthUseMap1(head); - int i2 = maxWidthNoMap1(head); + int i2 = maxWidthNoMap2(head); if (i1 != i2) { System.out.println("i1: " + i1 + " i2: " + i2); break; diff --git a/src/leo/class08_12/IsCBT.java b/src/leo/class08_12/IsCBT.java index 340aa3d..b01a624 100644 --- a/src/leo/class08_12/IsCBT.java +++ b/src/leo/class08_12/IsCBT.java @@ -15,6 +15,199 @@ import java.util.Queue; */ public class IsCBT { + static class ByQueue{ + public static boolean isCBT(Node head) { + if (head == null) { + return true; + } + Queue queue = new LinkedList<>(); + queue.offer(head); + boolean leaf = false; + Node cur = null; + Node l = null; + Node r = null; + while (!queue.isEmpty()) { + cur = queue.poll(); + l = cur.left; + r = cur.right; + if ( //是叶节点并且有子孩子 + (leaf && (l != null || r != null)) + || + (l == null && r != null)//当没有左节点时 + ) { + return false; + } + if (l != null) { + queue.offer(l); + } + if (r != null) { + queue.offer(r); + } + if (l == null || r == null) { + leaf = true; + } + } + return true; + } + + public static boolean isCBT1(Node head){ + if (head == null) { + return true; + } + Queue queue = new LinkedList<>(); + queue.offer(head); + Node cur; + Node l = null; + Node r = null; + boolean leaf = false; + while (!queue.isEmpty()) { + cur = queue.poll(); + l = cur.left; + r = cur.right; + if (l!= null) { + queue.offer(l); + } + if (r != null) { + queue.offer(r); + } + if ((leaf && + (l != null || r != null)) + || (l == null && r != null) + ) { + + return false; + } + if (l == null || r == null) { + leaf = true; + } + } + return true; + } + } + + static class Recursion{ + public static boolean isCBT(Node head) { + return process(head).isCBT; + + } + public static Info process(Node node){ + if (node == null) { + return new Info(true, true, 0); + } + Info left = process(node.left); + Info right = process(node.right); + int height = Math.max(left.height, right.height) + 1; + boolean isFull = left.isFull && right.isFull && right.height == left.height; + boolean isCBT = false; + if (isFull) { + isCBT = true; + }else{ + //如果左右都不是满二叉树的情况下 + //左树是满二叉树并且右树是满二叉树 + if (left.isCBT && right.isCBT) { + //左树是二叉树,右树是满二叉树,左树的高度比右树高度大1 + if (left.isCBT && right.isFull && left.height == right.height + 1) { + isCBT = true; + } + //左树是满二叉树,右树是满二叉树,左树的高度比右树高度大1 + if (left.isFull && right.isFull && left.height == right.height + 1) { + isCBT = true; + } + //左树是满二叉树,右树是二叉树,左树高度与右树高度一样 + if (left.isFull && right.isCBT && right.height == left.height) { + isCBT = true; + } + + } + } + return new Info(isFull, isCBT, height); + } + static class Info{ + boolean isFull; + boolean isCBT; + int height; + public Info(boolean f,boolean c,int h){ + this.isFull = f; + this.isCBT = c; + this.height = h; + } + } + } + + static class Recursion1{ + public static boolean isCBT(Node head){ + return process(head).isCBT; + } + public static class Info{ + public boolean isFull; + public boolean isCBT; + public int height; + public Info(boolean f,boolean c,int h){ + this.isFull = f; + this.isCBT = c; + this.height = h; + + } + } + public static Info process(Node node){ + if (node == null){ + return new Info(true, true, 0); + } + Info left = process(node.left); + Info right = process(node.right); + boolean isFull = left.isFull && right.isFull && left.height == right.height; + int height = Math.max(left.height, right.height) + 1; + boolean isCBT = isFull; + if (left.isCBT && right.isFull && left.height == right.height + 1) { + isCBT = true; + } else if (left.isFull && right.isFull && left.height == right.height + 1){ + isCBT = true; + } else if (left.isFull && right.isCBT && left.height == right.height) { + isCBT = true; + } + return new Info(isFull, isCBT, height); + } + } + + static class Recursion2{ + static boolean isCBT(Node head) { + + return process(head).isCBT; + } + + static Info process(Node node){ + if (node == null) { + return new Info(true, true, 0); + } + Info left = process(node.left); + Info right = process(node.right); + boolean isFull = left.isFull && right.isFull && right.height == left.height; + int height = Math.max(right.height, left.height) + 1; + boolean isCBT = isFull; + if (left.isFull && right.isCBT && left.height == right.height) { + isCBT = true; + } else if (left.isCBT && right.isFull && left.height == right.height + 1) { + isCBT = true; + } else if (left.isFull && right.isFull && left.height == right.height + 1) { + isCBT = true; + } + return new Info(isFull, isCBT, height); + } + + static class Info { + boolean isFull; + boolean isCBT; + int height; + + public Info(boolean isFull, boolean isCBT, int h) { + this.isFull = isFull; + this.isCBT = isCBT; + this.height = h; + } + } + } + + public static void main(String[] args){ int level = 4; int range = 100; @@ -24,7 +217,7 @@ public class IsCBT { for (int i = 0; i < testTime; i++) { Node head = generateRandomNode(level, range); boolean queue = ByQueue.isCBT1(head); - boolean recursion = Recursion.isCBT(head); + boolean recursion = Recursion2.isCBT(head); if (queue != recursion) { System.out.println("fuck!!!"); break; @@ -49,121 +242,3 @@ public class IsCBT { } -class ByQueue{ - public static boolean isCBT(Node head) { - if (head == null) { - return true; - } - Queue queue = new LinkedList<>(); - queue.offer(head); - boolean leaf = false; - Node cur = null; - Node l = null; - Node r = null; - while (!queue.isEmpty()) { - cur = queue.poll(); - l = cur.left; - r = cur.right; - if ( //是叶节点并且有子孩子 - (leaf && (l != null || r != null)) - || - (l == null && r != null)//当没有左节点时 - ) { - return false; - } - if (l != null) { - queue.offer(l); - } - if (r != null) { - queue.offer(r); - } - if (l == null || r == null) { - leaf = true; - } - } - return true; - } - - public static boolean isCBT1(Node head){ - if (head == null) { - return true; - } - Queue queue = new LinkedList<>(); - queue.offer(head); - Node cur; - Node l = null; - Node r = null; - boolean leaf = false; - while (!queue.isEmpty()) { - cur = queue.poll(); - l = cur.left; - r = cur.right; - if (l!= null) { - queue.offer(l); - } - if (r != null) { - queue.offer(r); - } - if ((leaf && - (l != null || r != null)) - || (l == null && r != null) - ) { - - return false; - } - if (l == null || r == null) { - leaf = true; - } - } - return true; - } -} - -class Recursion{ - public static boolean isCBT(Node head) { - return process(head).isCBT; - - } - public static Info process(Node node){ - if (node == null) { - return new Info(true, true, 0); - } - Info left = process(node.left); - Info right = process(node.right); - int height = Math.max(left.height, right.height) + 1; - boolean isFull = left.isFull && right.isFull && right.height == left.height; - boolean isCBT = false; - if (isFull) { - isCBT = true; - }else{ - //如果左右都不是满二叉树的情况下 - //左树是满二叉树并且右树是满二叉树 - if (left.isCBT && right.isCBT) { - //左树是二叉树,右树是满二叉树,左树的高度比右树高度大1 - if (left.isCBT && right.isFull && left.height == right.height + 1) { - isCBT = true; - } - //左树是满二叉树,右树是满二叉树,左树的高度比右树高度大1 - if (left.isFull && right.isFull && left.height == right.height + 1) { - isCBT = true; - } - //左树是满二叉树,右树是二叉树,左树高度与右树高度一样 - if (left.isFull && right.isCBT && right.height == left.height) { - isCBT = true; - } - - } - } - return new Info(isFull, isCBT, height); - } - static class Info{ - boolean isFull; - boolean isCBT; - int height; - public Info(boolean f,boolean c,int h){ - this.isFull = f; - this.isCBT = c; - this.height = h; - } - } -} diff --git a/src/leo/class08_12/MaxDistance.java b/src/leo/class08_12/MaxDistance.java index 3cf4d92..8684803 100644 --- a/src/leo/class08_12/MaxDistance.java +++ b/src/leo/class08_12/MaxDistance.java @@ -1,7 +1,5 @@ package leo.class08_12; -import class08_12.Code08_MaxDistance; - import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; diff --git a/src/leo/class09_13/LowestAncestor.java b/src/leo/class09_13/LowestAncestor.java new file mode 100644 index 0000000..0519b19 --- /dev/null +++ b/src/leo/class09_13/LowestAncestor.java @@ -0,0 +1,54 @@ +package leo.class09_13; + +/** + * @author Leo + * @ClassName lowestAncestor + * @DATE 2020/12/16 8:59 上午 + * @Description + * 给定一棵二叉树的头节点head,和另外两个节点a和b。 + * 返回a和b的最低公共祖先 + */ +public class LowestAncestor { + + static class Code { + static Node lowestAncestor(Node head, Node a, Node b) { + + return process(head, a, b).ans; + } + + static Info process(Node node, Node a, Node b) { + if (node == null) { + return new Info(null, false, false); + } + Info left = process(node, a, b); + Info right = process(node, a, b); + boolean findA = left.findA || right.findA || a == node; + boolean findB = left.findB || right.findB || b == node; + Node ans = null; + if (left.ans != null) { + ans = left.ans; + } + if (right.ans != null) { + ans = right.ans; + } + if (ans == null) { + if (findA && findB) { + ans = node; + } + } + return new Info(ans, findA, findB); + } + + static class Info { + Node ans; + boolean findA; + boolean findB; + + public Info(Node ans, boolean findA, boolean findB) { + this.ans = ans; + this.findA = findA; + this.findB = findB; + } + } + } +} diff --git a/src/leo/class09_13/LowestLexicography.java b/src/leo/class09_13/LowestLexicography.java new file mode 100644 index 0000000..1e0510e --- /dev/null +++ b/src/leo/class09_13/LowestLexicography.java @@ -0,0 +1,10 @@ +package leo.class09_13; + +/** + * @author Leo + * @ClassName LowestLexicography + * @DATE 2020/12/17 2:01 下午 + * @Description + */ +public class LowestLexicography { +} diff --git a/src/leo/class09_13/MaxHappy.java b/src/leo/class09_13/MaxHappy.java new file mode 100644 index 0000000..dd7e4ed --- /dev/null +++ b/src/leo/class09_13/MaxHappy.java @@ -0,0 +1,172 @@ +package leo.class09_13; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Leo + * @ClassName MaxHappy + * @DATE 2020/12/16 9:01 上午 + * @Description 派对的最大快乐值 + * 公司的每个员工都符合 Employee 类的描述。整个公司的人员结构可以看作是一棵标准的、 没有环的多叉树。 + * 树的头节点是公司唯一的老板。除老板之外的每个员工都有唯一的直接上级。 + * 叶节点是没有任何下属的基层员工(subordinates列表为空),除基层员工外,每个员工都有一个或多个直接下级。 + * 这个公司现在要办party,你可以决定哪些员工来,哪些员工不来,规则: + * 1.如果某个员工来了,那么这个员工的所有直接下级都不能来 + * 2.派对的整体快乐值是所有到场员工快乐值的累加 + * 3.你的目标是让派对的整体快乐值尽量大 + * 给定一棵多叉树的头节点boss,请返回派对的最大快乐值 + */ +public class MaxHappy { + + static class Code { + public static int maxHappy(Employee node){ + if (node == null) { + return 0; + } + Info info = process(node); + return Math.max(info.no, info.yes); + + } + public static Info process(Employee node) { + if (node == null) { + return new Info(0, 0); + } + int yes = node.happy; + int no = 0; + + List nexts = node.nexts; + for (Employee next : nexts) { + Info nextInfo = process(next); + yes += nextInfo.no; + no += Math.max(nextInfo.yes, nextInfo.no); + } + return new Info(no, yes); + } + + static class Info { + int no; + int yes; + + public Info(int no, int yes) { + this.no = no; + this.yes = yes; + } + } + } + + static class Code1 { + static int maxHappy(Employee employee) { + if (employee == null) { + return 0; + } + Info info = process(employee); + return Math.max(info.no, info.yes); + } + + static Info process(Employee e) { + if (e == null) { + return new Info(0, 0); + } + int yes = e.happy; + int no = 0; + List nexts = e.nexts; + for (Employee next : nexts) { + Info info = process(next); + yes += info.no; + no += Math.max(info.no, info.yes); + } + + return new Info(yes, no); + } + + static class Info { + int yes; + int no; + + public Info(int yes, int no) { + this.yes = yes; + this.no = no; + } + } + } + + + static class Logarithm { + public static int maxHappy(Employee employee) { + if (employee == null) { + return 0; + } + return process(employee,false); + } + static int process(Employee e,boolean verify) { + + if (verify) { + int ans = 0; + List nexts = e.nexts; + for (Employee next : nexts) { + ans += process(next, false); + } + return ans; + } + int yes = e.happy; + int no = 0; + List nexts = e.nexts; + for (Employee next : nexts) { + yes += process(next, true); + no += process(next, false); + } + return Math.max(yes, no); + + } + } + + + + public static void main(String[] args){ + int maxLevel = 5; + int listSize = 4; + int range = 1000; + int testTime = 10000; + System.out.println("start"); + for (int i = 0; i < testTime; i++) { + Employee head = new Employee((int) (range * Math.random() + 1)); + generateNexts(head, 1, maxLevel, listSize, range); + int i1 = Code1.maxHappy(head); + int i2 = Logarithm.maxHappy(head); + if (i1 != i2) { + System.out.println("FUCK!!!"); + break; + } + } + System.out.println("end"); + + + + } + + static void generateNexts(Employee node, int i, int n, int s, int r) { + if (i > n) { + return; + } + int nextSize = (int) (s * Math.random()) + 1; + for (int j = 0; j < nextSize; j++) { + Employee e = new Employee((int) (r * Math.random() + 1)); + node.nexts.add(e); + generateNexts(e, i + 1, n, s, r); + } + } + static class Employee { + public int happy; + public List nexts; + + public Employee(int h) { + happy = h; + nexts = new ArrayList<>(); + } + + } +} + + + diff --git a/src/leo/class09_13/MaxSubBSTHead.java b/src/leo/class09_13/MaxSubBSTHead.java new file mode 100644 index 0000000..a6f345e --- /dev/null +++ b/src/leo/class09_13/MaxSubBSTHead.java @@ -0,0 +1,204 @@ +package leo.class09_13; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/** + * @author Leo + * @ClassName MaxSubBSTHead + * @DATE 2020/12/15 10:33 下午 + * @Description + * 给定一棵二叉树的头节点head, + * 返回这颗二叉树中最大的二叉搜索子树的头节点 + */ +public class MaxSubBSTHead { + static class Code { + public static Node maxSubBSTHead(Node head) { + if (head == null) { + return null; + } + return process(head).maxSubSizeHead; + } + + public static Info process(Node node){ + if (node == null) { + return null; + } + Info left = process(node.left); + Info right = process(node.right); + Node maxSubBSTHead = null; + int maxSubSize = 0; + int max = node.value; + int min = node.value; + if (left != null) { + max = Math.max(max, left.max); + min = Math.min(min, left.min); + maxSubSize = left.maxSubSize; + maxSubBSTHead = left.maxSubSizeHead; + } + if (right != null) { + max = Math.max(max, right.max); + min = Math.min(min, right.min); + if (right.maxSubSize > maxSubSize) { + maxSubSize = right.maxSubSize; + maxSubBSTHead = right.maxSubSizeHead; + } + + } + if ( + (left == null ? true : left.max < node.value) && + (right == null ? true : right.min > node.value) && + (left == null ? true : left.maxSubSizeHead == node.left) && + (right == null ? true : right.maxSubSizeHead == node.right) + ) { + + maxSubSize = (left == null ? 0 : left.maxSubSize) + (right == null ? 0 : right.maxSubSize) + 1; + maxSubBSTHead = node; + } + return new Info(maxSubSize, maxSubBSTHead, max, min); + } + + static class Info{ + int maxSubSize; + Node maxSubSizeHead; + int max ; + int min; + + public Info(int maxSubSize, Node maxSubSizeHead, int max, int min) { + this.maxSubSize = maxSubSize; + this.maxSubSizeHead = maxSubSizeHead; + this.max = max; + this.min = min; + } + } + } + + static class Code1 { + + static Node maxSubBSTHead(Node head) { + if (head == null) { + return null; + } + return process(head).maxSubHead; + } + + public static Info process(Node node) { + if (node == null) { + return null; + } + Info left = process(node.left); + Info right = process(node.right); + Node maxSubHead = null; + int maxSubSize = 0; + int max = node.value; + int min = node.value; + if (left != null) { + max = Math.max(max, left.max); + min = Math.min(min, left.min); + maxSubSize = left.maxSubSize; + maxSubHead = left.maxSubHead; + } + if (right != null) { + max = Math.max(max, right.max); + min = Math.min(min, right.min); + if (right.maxSubSize>maxSubSize){ + maxSubHead = right.maxSubHead; + maxSubSize = right.maxSubSize; + } + } + if ((left == null ? true : left.max < node.value) && + (right == null ? true : right.min > node.value) && + (left == null ? true : left.maxSubHead == node.left) && + (right == null ? true : right.maxSubHead == node.right) + ) { + maxSubHead = node; + maxSubSize = (left == null ? 0 : left.maxSubSize) + (right == null ? 0 : right.maxSubSize) + 1; + } + Map set = new HashMap(); + return new Info(maxSubHead, maxSubSize, max, min); + + } + static class Info { + Node maxSubHead; + int maxSubSize; + int max ; + int min; + + public Info(Node maxSubHead, int maxSubSize, int max, int min) { + this.maxSubHead = maxSubHead; + this.maxSubSize = maxSubSize; + this.max = max; + this.min = min; + } + } + } + + static class Logarithm { + public static int getBSTSize(Node head) { + if (head == null) { + return 0; + } + ArrayList arr = new ArrayList<>(); + in(head, arr); + for (int i = 1; i < arr.size(); i++) { + if (arr.get(i).value <= arr.get(i - 1).value) { + return 0; + } + } + return arr.size(); + } + + public static void in(Node head, ArrayList arr) { + if (head == null) { + return; + } + in(head.left, arr); + arr.add(head); + in(head.right, arr); + } + + public static Node maxSubBSTHead(Node head) { + if (head == null) { + return null; + } + if (getBSTSize(head) != 0) { + return head; + } + Node leftAns = maxSubBSTHead(head.left); + Node rightAns = maxSubBSTHead(head.right); + return getBSTSize(leftAns) >= getBSTSize(rightAns) ? leftAns : rightAns; + } + } + + + // for test + public static Node generateRandomBST(int maxLevel, int maxValue) { + return generate(1, maxLevel, maxValue); + } + + // for test + public static Node generate(int level, int maxLevel, int maxValue) { + if (level > maxLevel || Math.random() < 0.5) { + return null; + } + Node head = new Node((int) (Math.random() * maxValue)); + head.left = generate(level + 1, maxLevel, maxValue); + head.right = generate(level + 1, maxLevel, maxValue); + return head; + } + + public static void main(String[] args) { + int maxLevel = 4; + int maxValue = 100; + int testTimes = 1000000; + for (int i = 0; i < testTimes; i++) { + Node head = generateRandomBST(maxLevel, maxValue); + if (Code1.maxSubBSTHead(head) != Logarithm.maxSubBSTHead(head)) { + System.out.println("Oops!"); + } + } + System.out.println("finish!"); + } +} diff --git a/src/leo/class09_13/Node.java b/src/leo/class09_13/Node.java new file mode 100644 index 0000000..9ed3f9f --- /dev/null +++ b/src/leo/class09_13/Node.java @@ -0,0 +1,16 @@ +package leo.class09_13; + +/** + * @author Leo + * @ClassName Node + * @DATE 2020/12/11 3:31 下午 + * @Description 二叉树 + */ +public class Node { + int value; + Node left; + Node right; + protected Node(int v){ + this.value = v; + } +}