按照前序遍历和中序遍历构建二叉树

转载自:http://blog.csdn.net/sbitswc/article/details/26433051

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

There is an example.
        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder:  {7}, {10,4,3,1}, {2,8,11}
inorder:     {4,10,3,1}, {7}, {11, 8,2}

        _______7______
       /              \
    {4,10,3,1}       {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.
2nd round
left part                                                                            right part
preorder: {10}, {4}, {3,1}                                              {2}, {8,11}
inorder:  {4}, {10}, {3,1}                                                {11,8}, {2}


        _______7______
       /              \
    __10__          ___2
   /      \        /
   4      {3,1}   {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.

Same way to split {3,1} and {11,8}, yo will get the complete tree now.

        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
So, simulate this process from bottom to top with recursion as following code.
c++

  1. TreeNode *BuildTreePI(  
  2.     vector<int> &preorder,  
  3.     vector<int> &inorder,  
  4.     int p_s, int p_e,  
  5.     int i_s, int i_e){  
  6.     if(p_s > p_e) return NULL;  
  7.     int pivot = preorder[p_s];  
  8.     int i = i_s;  
  9.     for(;i<i_e;i++){  
  10.         if(inorder[i] == pivot)  
  11.             break;  
  12.     }  
  13.     int length1 = i-i_s-1;  
  14.     int length2 = i_e-i-1;  
  15.     TreeNode* node = new TreeNode(pivot);  
  16.     node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);  
  17.     node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);  
  18.     return node;  
  19. }  
  20. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {  
  21.     return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);  
  22. }  

java

  1. public TreeNode buildTree(int[] preorder, int[] inorder) {  
  2.         return buildPI(preorder, inorder, 0, preorder.length-10, inorder.length-1);  
  3.     }  
  4.     public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){  
  5.         if(p_s>p_e)  
  6.             return null;  
  7.         int pivot = preorder[p_s];  
  8.         int i = i_s;  
  9.         for(;i<i_e;i++){  
  10.             if(inorder[i]==pivot)  
  11.                 break;  
  12.         }  
  13.         TreeNode node = new TreeNode(pivot);  
  14.         int lenLeft = i-i_s;  
  15.         node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);  
  16.         node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);  
  17.         return node;  
  18.     } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值