Construct Full Binary Tree from given preorder and postorder traversals

package tree;

public class ConstructFullBinaryTree {

	/**
	 * Construct Full Binary Tree from given preorder and postorder traversals
Given two arrays that represent preorder and postorder traversals of a full binary tree, construct the binary tree.

A Full Binary Tree is a binary tree where every node has either 0 or 2 children

Following are examples of Full Trees.

        1
      /   \
    2       3
  /  \     /  \
 4    5   6    7


       1
     /   \
   2      3
        /   \  
       4     5
           /   \  
          6    7
                  

          1
        /   \
      2       3
    /  \     /  \
   4    5   6    7
 /  \  
8    9 

It is not possible to construct a general Binary Tree from preorder and postorder 
traversals (See this). But if know that the Binary Tree is Full, we can construct the
 tree without ambiguity. Let us understand this with the help of following example.

Let us consider the two given arrays as pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7}
 and post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
In pre[], the leftmost element is root of tree. Since the tree is full and array size is more than 1. 
The value next to 1 in pre[], must be left child of root. So we know 1 is root and 2 is left child. 
How to find the all nodes in left subtree? We know 2 is root of all nodes in left subtree. All nodes
 before 2 in post[] must be in left subtree. Now we know 1 is root, elements {8, 9, 4, 5, 2} are in 
 left subtree, and the elements {6, 7, 3} are in right subtree.


                  1
                /   \
               /      \
     {8, 9, 4, 5, 2}     {6, 7, 3}
We recursively follow the above approach and get the following tree.

          1
        /   \
      2       3
    /  \     /  \
   4    5   6    7
  / \  
 8   9 

	 * @param args
	 */
	public static int idx;
	public static TreeNode construt(int[] pre,int[] post,int left,int right,int len){
		if(idx==len||left>right) return null;
	    TreeNode root = new TreeNode(pre[idx]);
	    if(left==right){
	    	idx++;
	    	return root;
	    }
	    idx++;
	    int i=left;
	    for(;i<=right;i++){
	    	if(pre[idx]==post[i]){
	    		break;
	    	}
	    }
	    root.left = construt(pre, post, left, i, len);
	    root.right = construt(pre, post, i+1, right-1, len);
	    return root;
		
	}
	public static void print(TreeNode root){
		if(root==null) return;
		System.out.print(root.value+" ");
		print(root.left);
		print(root.right);
	}
	public static void printin(TreeNode root){
		if(root==null) return;
		
		printin(root.left);
		System.out.print(root.value+" ");
		printin(root.right);
	}
	public static void main(String[] args) {
		
		int pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7};
		int post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
		TreeNode root = construt(pre, post, 0, post.length-1, pre.length);
		print(root);
		System.out.println();
		printin(root);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值