-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchTree.java
More file actions
66 lines (55 loc) · 1.31 KB
/
SearchTree.java
File metadata and controls
66 lines (55 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package tree;
import model.BinaryTree;
import model.TreeNode;
/**
* @fun 搜索二叉树相关方法类
* @author shadow
* @Date 2016年9月14日上午11:25:21
* @version 1.0
* @since
**/
public class SearchTree {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
System.out.println("IS BST : " + new SearchTree().isBST(tree.getRoot()));
}
/**
* 判断树是否为搜索二叉树<br/>
* 原理:二叉树中序遍历,在遍历的过程中看节点值是否都是递增的即可。<br/>
* @param head
* @return
*/
public boolean isBST(TreeNode head){
if(head == null){
return true;
}
boolean res = true;
TreeNode pre = null;
TreeNode cur1 = head;
TreeNode cur2 = null;
while(cur1 != null){
cur2 = cur1.getLeft();
if(cur2 != null){
//得到最右边为右节点为空的树
while(cur2.getRight() != null && cur2.getRight() != cur1){
cur2 = cur2.getRight();
}
if(cur2.getRight() == null){
cur2.setRight(cur1);
cur1 = cur1.getLeft();
continue;
}else{
cur2.setRight(null);
}
}
if(pre != null && pre.getValue() > cur1.getValue()){
res = false;
}
pre = cur1;
System.out.print(" " + cur1.getValue());
cur1 = cur1.getRight();
}
System.out.println();
return res;
}
}