根据后序数组重建搜索二叉树
2017-11-26 17:03
435 查看
//根据后序数组重建搜索二叉树
public class PostTree{
//一、判断是否是后序数组
public static boolean isPostArray(int []arr )
{
if(arr==null||arr.length==0)
{
return false;
}
return isPost(arr,0,arr.length-1);
}
//递归调用
public static boolean isPost(int[]arr,int start,int end)
{
if(start==end)
{
return true;
}
int less=-1;
int more=end;
for(int i=start;i<end;i++)
{
if(arr[end]>arr[i])
{
less=i;
}else
{
more=more==end?i:more;
}
}
if(less==-1||more==end)
{
return isPost(arr,start,end-1);
}
if(less!=more-1)
{
return false;
}
return isPost(arr,start,less)&&isPost(arr,more,end-1);
}
//*****************************************************
//二、根据后序遍历数组重建搜索二叉树
//二叉树节点的定义
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data)
{
this.value=data;
}
}
//构建搜索二叉树
public static Node posArrayToBST(int[] posArr) {
if (posArr == null) {
return null;
}
return posToBST(posArr, 0, posArr.length - 1);
}
public static Node posToBST(int[] posArr, int start, int end) {
if (start > end) {
return null;
}
Node head = new Node(posArr[end]);
int less = -1;
int more = end;
for (int i = start; i < end; i++) {
if (posArr[end] > posArr[i]) {
less = i;
} else {
more = more == end ? i : more;
}
}
head.left = posToBST(posArr, start, less);
head.right = posToBST(posArr, more, end - 1);
return head;
}
// 先序遍历打印二叉树
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len)
{
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
//*************************************************************************
public static void main(String[] args)
{
//判断一个数组是否为后序二叉树遍历
int[]arr={2,1,3,6,5,7,4};
System.out.println(isPostArray(arr));
//打印搜索二叉树
printTree(posArrayToBST(arr));
}
}
相关文章推荐
- 二叉树问题---根据后序数组重建搜索二叉树
- 根据后序和中序遍历重建二叉树
- 剑指off-判断一个数组是不是搜索二叉树的后序遍历的序列
- 根据后序和中序遍历重建二叉树
- 二叉树问题---判断数组是否为某搜索二叉树的后序遍历的结果
- 七:重建二叉树(根据先序遍历(或者后序遍历)和中序遍历重建二叉树)
- 根据中序遍历和后序遍历重建二叉树
- 根据二叉树中序和后序(先序)遍历结果 重建二叉树
- 根据前序遍历(或者是后序遍历)和中序遍历得到的序列可以重建二叉树
- [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
- 根据二叉树前序遍历和中序遍历的结果,重建出该二叉树并后序遍历之 java代码
- 剑指offer 判断数组的后序遍历是否为搜索二叉树
- Python练手之根据前序和中序&根据中序和后序重建二叉树,输出前序、中序和后序遍历结果
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
- 【BST】根据前序遍历的数组构造搜索二叉树
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- 判断数组是否为搜索二叉树的后序遍历
- (剑指offer笔记)根据前序遍历和后序遍历重建二叉树
- 刷题之路----根据前序遍历和中序遍历或者后序遍历和中序遍历重建二叉树
- 根据二叉树的前序遍历序列和中序遍历序列求二叉树的后序遍历序列