forked from zq2599/blog_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
510 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package offer; | ||
|
||
/** | ||
* @program: leetcode | ||
* @description: | ||
* @author: [email protected] | ||
* @create: 2022-06-30 22:33 | ||
**/ | ||
public class L0033 { | ||
|
||
private boolean verify(int[] array, int start, int end) { | ||
// System.out.println("开始验证[" + start + "]-[" + end + "],总长[" + array.length + "]"); | ||
|
||
// 终止条件 | ||
if (start >= end) { | ||
return true; | ||
} | ||
|
||
int leftStart = start; | ||
// 注意,leftEnd的默认值必须是end-1,因为一旦不存在右子树,整段都是左子树 | ||
int leftEnd = end-1; | ||
|
||
int rightStart = start; | ||
int rightEnd = end - 1; | ||
|
||
boolean find = false; | ||
// 先划分左右子树 | ||
for (int i = start; i <= end - 1; i++) { | ||
// System.out.println("i=[" + i + "], array[i]=[" + array[i] + "], array[end]=" + array[end] + "]"); | ||
// 第一个比end大的元素,就是右子树 | ||
if (array[i] > array[end]) { | ||
find = true; | ||
// System.out.println("i=[" + i + "]"); | ||
leftEnd = i - 1; | ||
rightStart = i; | ||
break; | ||
} | ||
} | ||
|
||
// System.out.println("左[" + leftStart + "]-[" + leftEnd + "],右[" + rightStart + "]-[" + rightEnd + "]"); | ||
|
||
// 左子树都要比根节点小 | ||
for (int i = leftStart; i <= leftEnd; i++) { | ||
if (array[i] > array[end]) { | ||
// System.out.println("------1"); | ||
return false; | ||
} | ||
} | ||
|
||
// 确实存在右子树 | ||
if (find) { | ||
// 右子树都要比自己大 | ||
for (int i = rightStart; i <= rightEnd; i++) { | ||
if (array[i] < array[end]) { | ||
// System.out.println("------2"); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// 递归校验左子树 | ||
if (!verify(array, leftStart, leftEnd)) { | ||
// System.out.println("------3"); | ||
return false; | ||
} | ||
|
||
// 递归的校验右子树 | ||
return find ? verify(array, rightStart, rightEnd) : true; | ||
} | ||
|
||
public boolean verifyPostorder(int[] postorder) { | ||
return verify(postorder, 0, postorder.length - 1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// System.out.println(new L0033().verifyPostorder(new int[]{1,6,3,2,5})); // | ||
// false | ||
// System.out.println(""); | ||
|
||
// System.out.println(new L0033().verifyPostorder(new int[]{1,3,2,6,5})); // | ||
// false | ||
// System.out.println(""); | ||
|
||
// System.out.println(new L0033().verifyPostorder(new int[] { 4, 6, 7, 5 })); // true | ||
// System.out.println(""); | ||
|
||
System.out.println(new L0033().verifyPostorder(new int[] { 5, 2, -17, -11, 25, 76, 62, 98, 92, 61 })); // true | ||
System.out.println(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package practice; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Deque; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* @program: leetcode | ||
* @description: | ||
* @author: [email protected] | ||
* @create: 2022-06-30 22:33 | ||
**/ | ||
public class L0331 { | ||
|
||
// 9,3,4,#,#,1,#,#,2,#,6,#,# | ||
/* | ||
public boolean isValidSerialization(String preorder) { | ||
String[] array = preorder.split(","); | ||
Deque<String> deque = new LinkedList<String>(); | ||
Deque<String> temp = new LinkedList<String>(); | ||
// 入栈 | ||
for (int i=0;i<array.length;i++) { | ||
deque.offerLast(array[i]); | ||
} | ||
String lastLast = null;; | ||
String last = null; | ||
while (!deque.isEmpty()) { | ||
String current = deque.pollFirst(); | ||
// System.out.println("1. 取出了["+current+"]"); | ||
// 凑不齐三个就不用检查 | ||
if (null==lastLast) { | ||
// System.out.println("2. 凑不齐三个"); | ||
temp.offerLast(current); | ||
lastLast = last; | ||
last = current; | ||
continue; | ||
} | ||
// System.out.println("3. [" + lastLast + "]-[" + last + "]-[" + current + "]"); | ||
if (!"#".equals(lastLast) && "#".equals(last) && "#".equals(current)) { | ||
// System.out.println("4. [" + lastLast + "]-[" + last + "]-[" + current + "],这是个叶子结点,处理后将temp全部塞回去"); | ||
// 注意:刚才是从头顶取的,要从头顶塞回去 | ||
deque.offerFirst(current); | ||
// temp要丢掉两个 | ||
temp.pollLast(); | ||
temp.pollLast(); | ||
while(!temp.isEmpty()) { | ||
deque.offerFirst(temp.pollLast()); | ||
} | ||
// temp空了,两个变量也要清理 | ||
lastLast = null; | ||
last = null; | ||
} else { | ||
// System.out.println("5. 不是叶子节点"); | ||
temp.offerLast(current); | ||
lastLast = last; | ||
last = current; | ||
} | ||
} | ||
return 1==temp.size() && "#".equals(temp.pollFirst()); | ||
} | ||
*/ | ||
|
||
// 1. 根节点:0入,2出 | ||
// 2. 其他节点:1入,2出 | ||
// 3. null节点:1入,0出 | ||
|
||
public boolean isValidSerialization(String preorder) { | ||
// 空节点,特殊 | ||
if ("#".equals(preorder)) { | ||
return true; | ||
} | ||
|
||
int in = 0; | ||
int out = 0; | ||
|
||
String[] array = preorder.split(","); | ||
|
||
|
||
for (int i=0;i<array.length;i++) { | ||
|
||
if (0==i) { | ||
// 长度大于1的时候,根节点还要等于#,就是非法 | ||
if ("#".equals(array[i])) { | ||
return false; | ||
} | ||
|
||
// 1. 根节点:0入,2出 | ||
out = 2; | ||
continue; | ||
} | ||
|
||
|
||
if ("#".equals(array[i])) { | ||
// 3. null节点:1入,0出 | ||
in++; | ||
} else { | ||
// 2. 其他节点:1入,2出 | ||
in++; | ||
out += 2; | ||
} | ||
|
||
// 不到最后,入度一定是小于出度的 | ||
if (i<(array.length-1) && in>=out) { | ||
return false; | ||
} | ||
} | ||
|
||
return in==out; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new L0331().isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#")); | ||
// System.out.println(new L0331().isValidSerialization("#,#,#")); | ||
} | ||
} |
Oops, something went wrong.