modify code

master
algorithmzuo 3 years ago
parent 99940063b1
commit fb03c1258f

@ -2,27 +2,30 @@ package class13;
import java.util.LinkedList; import java.util.LinkedList;
// 测试链接 : https://leetcode.com/problems/check-completeness-of-a-binary-tree/
public class Code01_IsCBT { public class Code01_IsCBT {
public static class Node { // 不要提交这个类
public int value; public static class TreeNode {
public Node left; public int val;
public Node right; public TreeNode left;
public TreeNode right;
public Node(int data) { public TreeNode(int v) {
this.value = data; val = v;
} }
} }
public static boolean isCBT1(Node head) { public static boolean isCompleteTree1(TreeNode head) {
if (head == null) { if (head == null) {
return true; return true;
} }
LinkedList<Node> queue = new LinkedList<>(); LinkedList<TreeNode> queue = new LinkedList<>();
// 是否遇到过左右两个孩子不双全的节点 // 是否遇到过左右两个孩子不双全的节点
boolean leaf = false; boolean leaf = false;
Node l = null; TreeNode l = null;
Node r = null; TreeNode r = null;
queue.add(head); queue.add(head);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
head = queue.poll(); head = queue.poll();
@ -48,7 +51,7 @@ public class Code01_IsCBT {
return true; return true;
} }
public static boolean isCBT2(Node head) { public static boolean isCompleteTree2(TreeNode head) {
return process(head).isCBT; return process(head).isCBT;
} }
@ -64,7 +67,7 @@ public class Code01_IsCBT {
} }
} }
public static Info process(Node x) { public static Info process(TreeNode x) {
if (x == null) { if (x == null) {
return new Info(true, true, 0); return new Info(true, true, 0);
} }
@ -86,16 +89,16 @@ public class Code01_IsCBT {
} }
// for test // for test
public static Node generateRandomBST(int maxLevel, int maxValue) { public static TreeNode generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue); return generate(1, maxLevel, maxValue);
} }
// for test // for test
public static Node generate(int level, int maxLevel, int maxValue) { public static TreeNode generate(int level, int maxLevel, int maxValue) {
if (level > maxLevel || Math.random() < 0.5) { if (level > maxLevel || Math.random() < 0.5) {
return null; return null;
} }
Node head = new Node((int) (Math.random() * maxValue)); TreeNode head = new TreeNode((int) (Math.random() * maxValue));
head.left = generate(level + 1, maxLevel, maxValue); head.left = generate(level + 1, maxLevel, maxValue);
head.right = generate(level + 1, maxLevel, maxValue); head.right = generate(level + 1, maxLevel, maxValue);
return head; return head;
@ -106,8 +109,8 @@ public class Code01_IsCBT {
int maxValue = 100; int maxValue = 100;
int testTimes = 1000000; int testTimes = 1000000;
for (int i = 0; i < testTimes; i++) { for (int i = 0; i < testTimes; i++) {
Node head = generateRandomBST(maxLevel, maxValue); TreeNode head = generateRandomBST(maxLevel, maxValue);
if (isCBT1(head) != isCBT2(head)) { if (isCompleteTree1(head) != isCompleteTree2(head)) {
System.out.println("Oops!"); System.out.println("Oops!");
} }
} }

Loading…
Cancel
Save