已知二叉树前序,中序遍历,求后序遍历,java实现

来源:互联网 发布:淘宝ifashion怎么进入 编辑:程序博客网 时间:2024/06/10 03:43
  简单介绍一下思想,先看前序,前序遍历的第一个节点,就是该树的根。在中序中找到该根的位置,设为index,在中序遍历集合中,位于index之前的属于根的左子树,位于index之后的属于根的右子树。然后,对左右子数,遍历的调用该过程,就能将树结构还原。之后后序遍历也就不是问题了。
public class M {public static class Tree{public int value;public Tree left;public Tree right;public Tree(int value) {this.value=value;}}public static void main(String[] args){int[]preOrder={1,2,4,5,3,6,7};int[]midOrder={4,2,5,1,6,3,7};getBehindOrder(preOrder, midOrder);}public static void getBehindOrder(int[]pre,int[]mid){if (pre==null||mid==null||pre.length!=mid.length) {return;}Tree root=buildTree(pre, mid);behindOrder(root);}public static void behindOrder(Tree root){//后序遍历二叉树if (root==null) {return;}if (root.left!=null) {behindOrder(root.left);}if (root.right!=null) {behindOrder(root.right);}System.out.print(root.value+" ");}public static Tree buildTree(int[]preOrder,int[]midOrder){//根据前序和中序,建立二叉树int value=preOrder[0];int length=preOrder.length;Tree root=new Tree(value);root.left=root.right=null;if (preOrder.length==1) {return root;}int index=0;while(midOrder[index]!=value)index++;//此处还要考虑index==length-1的情况if (index>0) {//中序中,根节点左边的节点都属于左子树int[]leftSubPreOrder=new int[index];for(int i=0;i<leftSubPreOrder.length;i++){leftSubPreOrder[i]=preOrder[i+1];}int[]leftSubMidOrder=new int[index];for(int i=0;i<leftSubMidOrder.length;i++){leftSubMidOrder[i]=midOrder[i];}root.left=buildTree(leftSubPreOrder, leftSubMidOrder);}if(length-index-1>0){int[]rightSubMidOrder=new int[length-index-1];for(int i=0;i<rightSubMidOrder.length;i++){rightSubMidOrder[i]=midOrder[i+index+1];}int[]rightSubPreOrder=new int[length-index-1];for(int i=0;i<rightSubPreOrder.length;i++){rightSubPreOrder[i]=preOrder[i+index+1];}root.right=buildTree(rightSubPreOrder, rightSubMidOrder);}return root;}}

0 0
原创粉丝点击