第9题(树)
判断整数序列是不是二元查找树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
如果是返回true,否则返回false。

例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:

  

8
/ /
6 10
/ / / /
5 7 9 11


因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。

 

 

package com.microsoft;


public class BinarySearchTree {
private Node root;
public BinarySearchTree(int[]data){
for(int i=0;i<data.length;i++){
Node node=new Node();
node.setValue(data[i]);
insert(node);
}
}
public BinarySearchTree(int[]data,boolean reverse)throws Exception{
for(int i=data.length-1;i>=0;i--){
Node node=new Node();
node.value=data[i];
if(root==null){
root=node;
continue;
}
Node current=root;
while(current!=null){
if(node.value>current.value){
if(current.left!=null){
throw new Exception("该数组不是某二元查找树的后序遍历的结果");
}
if(current.right!=null){
current=current.right;
}else{
current.right=node;
current=null;
}
}else{
if(current.left!=null){
current=current.left;
}else{
current.left=node;
current=null;
}
}
}
}
}

/**
* 中序遍历
*/
public void inorderTreeWalk(){
innerInorderTreeWalk(root);
System.out.println();
}
/**
* 中序遍历
*/
private void innerInorderTreeWalk(Node node){
if(node!=null){
innerInorderTreeWalk(node.left);
System.out.print("-->"+node.value);
innerInorderTreeWalk(node.right);
}
}
/**
* 先序遍历
*/
public void preorderWalk(){
innerPreorderWalk(root);
System.out.println();
}
private void innerPreorderWalk(Node node){
System.out.print("-->"+node.value);
innerPreorderWalk(node.left);
innerPreorderWalk(node.right);
}
/**
* 后续遍历
*/
public void postorderWalk(){
innerPostorderWalk(root);
System.out.println();
}
private void innerPostorderWalk(Node node){
if(node==null){
return;
}
innerPostorderWalk(node.left);
innerPostorderWalk(node.right);
System.out.print("-->"+node.value);
}
/**
* 查找值为value的节点
* @param value
* @return
*/
public Node search(int value){
return innerSearch(root,value);
}
private Node innerSearch(Node node,int value){
if(node==null||node.value==value){
return node;
}
if(node.value>value){
return innerSearch(node.left,value);
}else{
return innerSearch(node.right,value);
}
}
/**
* 返回最小值节点
* @return
*/
public Node minimum(){
return innerMinimum(root);
}
private Node innerMinimum(Node node){
if(node.left!=null){
return innerMinimum(node.left);
}
return node;
}
/**
* 返回最大值节点
* @return
*/
public Node maximum(){
return innerMaximum(root);
}
private Node innerMaximum(Node node){
if(node.right!=null){
return innerMaximum(node.right);
}
return node;
}
/**
* 返回给定节点的后继结点
* @param node
* @return
*/
public Node successor(Node node){
if(node.right!=null){
return innerMinimum(node.right);
}
Node y=node.parent;
while(y!=null&&y.right==node){
node=y;
y=y.parent;
}
return y;
}
public void insert(int value){
Node node=new Node();
node.setValue(value);
insert(node);
}
/**
* 插入节点
* @param node
*/
public void insert(Node node){
Node y=null;
Node x=root;
while(x!=null){
y=x;
if(node.value<x.value){
x=x.left;
}else{
x=x.right;
}
}
node.parent=y;
if(y==null){
root=node;
}else if(y.value>node.value){
y.left=node;
}else{
y.right=node;
}
}

private void transplant(Node u,Node v){
if(u.parent==null){
root=v;
}else if(u==u.parent.left){
u.parent.left=v;
}else{
u.parent.right=v;
}
if(v!=null){
v.parent=u.parent;
}
}
public void delete(int value){
delete(search(value));
}
/**
* 删除节点
* @param node
*/
public void delete(Node node){
if(node.left==null){
transplant(node,node.right);
}else if(node.right==null){
transplant(node,node.left);
}else{
Node y=innerMinimum(node.right);
if(y.parent!=node){
transplant(y,y.right);
y.right=node.right;
y.right.parent=y;
}
transplant(node,y);
y.left=node.left;
y.left.parent=y;
}
}
private static class Node{
private Node left;
private Node right;
private Node parent;
private int value;
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}

}
public static void main(String[] args) throws Exception{
int [] data=new int[]{5,7,6,9,11,10,8};
BinarySearchTree tree=new BinarySearchTree(data,true);
tree.postorderWalk();
int [] data2=new int[]{7,4,6,5};
BinarySearchTree tree2=new BinarySearchTree(data2,true);
tree2.postorderWalk();

}

}