由于需要检查树中的每一个元素,结点的处理顺序可以是从叶子结点到根结点。这样当处理到结点本身时,其左子树和右字树为有效剪枝的BST。
static BianrySearchTreeNode PruneBST(BianrySearchTreeNode root, int A, int B){
if(root == null)
return null;
root.setLeft(PruneBST(root.getLeft(), A, B));
root.setRight(PruneBST(root.getRight(), A, B));
if(root.getData()>=A && root.getData()<=B)
return root;
if(root.getData()<A)
return root.getRight();
if(root.getData()>B)
return root.getLeft();
return root;
}