1 package my_basic.class_4;
2
3 import java.util.Stack;
4
5 import class_04.Code_07_IsBSTAndCBT.Node;
6
7 public class Code_07_IsSBTAndCBT {
8 public static class Node{
9 int value;
10 Node left;
11 Node right;
12 public Node(int value) {
13 this.value = value;
14 }
15 }
16
17 /**判断是否是搜索二叉树
18 * 中序遍历 有序
19 * @return
20 */
21 public static Boolean isSBT(Node head) {
22 if (head != null) {
23 Stack<Node> stack = new Stack<Node>();
24 int pre = Integer.MIN_VALUE;
25 while (!stack.isEmpty() || head!=null) {
26 if (head!=null) {
27 stack.add(head);
28 head = head.left;
29 }else {
30 head = stack.pop();
31 // System.out.print(head.value+" ");
32 if (head.value < pre) {
33 return false;
34 }
35 pre = head.value;
36 head = head.right;
37 }
38 }
39 }
40 return true;
41 }
42
43 public static void main(String[] args) {
44 Node head = new Node(4);
45 head.left = new Node(2);
46 head.right = new Node(6);
47 head.left.left = new Node(1);
48 head.left.right = new Node(3);
49 head.right.left = new Node(5);
50
51 System.out.println(isSBT(head));
52 }
53 }