|
| 1 | +package algorithm.basic04; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +/** |
| 8 | + * @Created by mood321 |
| 9 | + * @Date 2019/11/4 0004 |
| 10 | + * @Description TODO |
| 11 | + */ |
| 12 | +public class Code_07_IsBSTAndCBT { |
| 13 | + public static class Node { |
| 14 | + int data; |
| 15 | + Node left; |
| 16 | + Node right; |
| 17 | + |
| 18 | + public Node(int data) { |
| 19 | + this.data = data; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // 非递归版本中序遍历 |
| 24 | + public static boolean isBST1(Node head) { |
| 25 | + if (head == null) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + Stack<Node> stack = new Stack<>(); |
| 29 | + Node pre = new Node(Integer.MIN_VALUE); |
| 30 | + while (!stack.isEmpty() || head != null) { |
| 31 | + if (head != null) { |
| 32 | + stack.push(head); |
| 33 | + head = head.left; |
| 34 | + } else { |
| 35 | + Node pop = stack.pop(); |
| 36 | + if (pre.data > pop.data) |
| 37 | + return false; |
| 38 | + pre = pop; |
| 39 | + head = pop.right; |
| 40 | + |
| 41 | + } |
| 42 | + } |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + public static void main(String[] args) { |
| 47 | + Node head = new Node(4); |
| 48 | + head.left = new Node(2); |
| 49 | + head.right = new Node(6); |
| 50 | + head.left.left = new Node(1); |
| 51 | + head.left.right = new Node(3); |
| 52 | + head.right.left = new Node(5); |
| 53 | + |
| 54 | + /* printTree(head);*/ |
| 55 | + System.out.println(isBST(head));//morris 遍历 判断是否是搜索树 |
| 56 | + System.out.println(isBST1(head));// 非递归遍历 判断是否是搜索树 |
| 57 | + System.out.println(isBST2(head));// 递归遍历 判断是否是搜索树 |
| 58 | + System.out.println(isCBT(head));// 判断是否是一个 完全二叉树 |
| 59 | + |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 判断时候是完全二叉树 |
| 65 | + * 1. 有右子节点 无左子树 一定不是 |
| 66 | + * 2. 有左子树 无右子树 接下来所有节点必须是叶子节点 才是完全二叉树 |
| 67 | + * @param head |
| 68 | + * @return |
| 69 | + */ |
| 70 | + public static boolean isCBT(Node head) { |
| 71 | + if(head==null){ |
| 72 | + return true; |
| 73 | + } |
| 74 | + Queue<Node> queue = new LinkedList<>(); |
| 75 | + queue.offer(head); |
| 76 | + Node l=null; |
| 77 | + Node r=null; |
| 78 | + // |
| 79 | + boolean flag=false; |
| 80 | + while (!queue.isEmpty()){ |
| 81 | + head = queue.poll(); |
| 82 | + l=head.left; |
| 83 | + r=head.right; |
| 84 | + // 在第一种情况下 左为空 右不为空 不是 |
| 85 | + // 在第二种情况下 已经发生 节点必须是叶子节点 进入这种情况 左子不为空 |
| 86 | + if ((flag && (l != null || r != null)) || (l == null && r != null)) |
| 87 | + return false; |
| 88 | + |
| 89 | + // 逻辑与上面相等 |
| 90 | + /*if(l == null && r != null) |
| 91 | + return false; |
| 92 | + if(flag){ |
| 93 | + if(l != null || r != null) |
| 94 | + return false; |
| 95 | + }*/ |
| 96 | + // |
| 97 | + if(l!=null) |
| 98 | + queue.offer(l); |
| 99 | + if(r!=null) |
| 100 | + queue.offer(r); |
| 101 | + } |
| 102 | + |
| 103 | + return true; |
| 104 | + |
| 105 | + } |
| 106 | + public static boolean isBST(Node head) { |
| 107 | + if (head == null) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + boolean res = true; |
| 111 | + Node pre = null; |
| 112 | + Node cur1 = head; |
| 113 | + Node cur2 = null; |
| 114 | + while (cur1 != null) { |
| 115 | + cur2 = cur1.left; |
| 116 | + if (cur2 != null) { |
| 117 | + while (cur2.right != null && cur2.right != cur1) { |
| 118 | + cur2 = cur2.right; |
| 119 | + } |
| 120 | + if (cur2.right == null) { |
| 121 | + cur2.right = cur1; |
| 122 | + cur1 = cur1.left; |
| 123 | + continue; |
| 124 | + } else { |
| 125 | + cur2.right = null; |
| 126 | + } |
| 127 | + } |
| 128 | + if (pre != null && pre.data > cur1.data) { |
| 129 | + res = false; |
| 130 | + } |
| 131 | + pre = cur1; |
| 132 | + cur1 = cur1.right; |
| 133 | + } |
| 134 | + return res; |
| 135 | + } |
| 136 | + |
| 137 | + // 递归 |
| 138 | + private static boolean isBST2(Node head) { |
| 139 | + if (head == null) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + Boolean[] res = {true}; |
| 143 | + checkNode(head, head.data, false, res); |
| 144 | + return res[0]; |
| 145 | + } |
| 146 | + |
| 147 | + private static void checkNode(Node head, int minValue, boolean flag, Boolean[] res) { |
| 148 | + if (head == null) { |
| 149 | + return; |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + checkNode(head.left, head.data, false, res); |
| 154 | + if (!res[0] ) { |
| 155 | + return; |
| 156 | + } |
| 157 | + if (!flag) { |
| 158 | + if (minValue < head.data) |
| 159 | + res[0] = false; |
| 160 | + } else { |
| 161 | + if (minValue > head.data) |
| 162 | + res[0] = false; |
| 163 | + } |
| 164 | + |
| 165 | + checkNode(head.right, head.data, true, res); |
| 166 | + if (!res[0] ) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | +} |
0 commit comments