用英文描述一下思路吧...英语还是要多学习,写的不清楚将就着看吧....
We are given a preorder list of tree, if it is a BST, then give the postorder list.
I got some clue from the algorithm "how to get a tree from its preorder and inorder lists". It was shown by http://blog.csdn.net/hinyunsin/article/details/6315502
if we get a preorder of BST like this:
8 6 5 7 10 8 11As we all know, the BST has the property that left nodes are smaller than the root. And the right are bigger. So we can get from the example that 8 is the root value, and 6 5 7 are on the left, 10 8 11 are on the right.
So we can get the tree by Recursion. as follow (assume the left is smaller):
public static Node GetNode(List<Integer> list)
{
Node root = new Node();
if(list.size() <= 0)
return null;
root.value = list.get(0);
//find index which cut left and right.
int index = 1;
int nSize = list.size();
for(; index < nSize; index ++)
{
if(list.get(index) - root.value >= 0)
break;
}
if(index == 1)
root.pLeft = null;
else
root.pLeft = GetNode(list.subList(1, index));
if(index == nSize)
root.pRight = null;
else
root.pRight = GetNode(list.subList(index, nSize));
return root;
}
We can get a tree through above method if we assume it is a BST. And the second thing we should do is "whether it is a BST" And I also use the property above. as follow:
public static boolean IsTreeBST(Node pNode, int min, int max)
{
if(pNode == null)
return true;
if(pNode.value < min || pNode.value >= max)
return false;
return
IsTreeBST(pNode.pLeft, min, pNode.value) &&
IsTreeBST(pNode.pRight, pNode.value, max);
}
There are also some other algorithm to judge a BST : http://blog.csdn.net/sgbfblog/article/details/7771096
Ok. that is all.
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:7 8 6 5 7 10 8 11Sample Output 1:
YES 5 7 6 8 11 10 8Sample Input 2:
7 8 10 11 8 6 7 5Sample Output 2:
YES 11 8 10 7 5 6 8Sample Input 3:
7 8 6 8 5 10 9 11Sample Output 3:
NO
answer, and all test cases were passed.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
public static final int MIN = -2147483648;
public static final int MAX = 2147483647;
public static class Node{
int value;
Node pLeft;
Node pRight;
}
//mode present the increase or decrease order.
public static Node GetNode(List<Integer> list, int mode)
{
Node root = new Node();
if(list.size() <= 0)
return null;
root.value = list.get(0);
if(list.size() == 1)
{
root.pLeft = null;
root.pRight = null;
return root;
}
//find index which cut left and right.
int index = 1;
int nSize = list.size();
for(; index < nSize; index ++)
{
if(((mode < 0) && (list.get(index) - root.value) < 0)
|| ((mode > 0) && (list.get(index) - root.value) >= 0))
break;
}
if(index == 1)
root.pLeft = null;
else
root.pLeft = GetNode(list.subList(1, index), mode);
if(index == nSize)
root.pRight = null;
else
root.pRight = GetNode(list.subList(index, nSize), mode);
return root;
}
//judge whether the tree is bst
public static boolean IsTreeBST(Node pNode, int min, int max, int mode)
{
if(pNode == null)
return true;
if(pNode.value < min || pNode.value >= max)
return false;
if(mode > 0)/*左小右大的模式*/
{
return
IsTreeBST(pNode.pLeft, min, pNode.value, mode) &&
IsTreeBST(pNode.pRight, pNode.value, max, mode);
}
else
{
return
IsTreeBST(pNode.pLeft, pNode.value, max, mode) &&
IsTreeBST(pNode.pRight, min, pNode.value, mode);
}
}
//post
public static void postTree(Node pNode, List<Integer> postList)
{
if(pNode == null)
return;
postTree(pNode.pLeft, postList);
postTree(pNode.pRight, postList);
postList.add(pNode.value);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int nNum = sc.nextInt();
List<Integer> treeList = new ArrayList<Integer>();
for(int i = 0; i < nNum; i ++)
{
treeList.add(sc.nextInt());
}
boolean isPrin = false;
for(int mode = 1; mode >= -1; mode = mode -2)
{
Node root = GetNode(treeList, mode);
if(IsTreeBST(root, MIN, MAX, mode))
{
System.out.print("YES\n");
List<Integer> postList = new ArrayList<Integer>();
postTree(root, postList);
MyPrint(postList);
isPrin = true;
break;
}
}
if(!isPrin)
System.out.print("NO");
sc.close();
}
//
public static void MyPrint(List<Integer> list)
{
int nSize = list.size();
for(int i = 0; i < nSize; i ++)
{
if(i == nSize - 1)
System.out.print(list.get(i));
else
System.out.print(list.get(i) + " ");
}
}
}