码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode前序遍历和中序遍历树构造二叉树

时间:2015-06-14 16:41:30      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

样例

给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:

  2
 / 1   
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
 

class Solution {
    /**
     *@param preorder : A list of integers that preorder traversal of a tree
     *@param inorder : A list of integers that inorder traversal of a tree
     *@return : Root of a tree
     */
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        // write your code here
        
        int len1 = preorder.size();
        int len2 = inorder.size();
        if (len1 <= 0 || len2 <= 0) {
            return nullptr;
        }
        return  dp(preorder,inorder);
    
        
    }
    TreeNode *dp(vector<int> &porder, vector<int> &iorder) { 
        int len1 = porder.size();
        int len2 = iorder.size();
        if (len1 <= 0 || len2 <= 0) {
            return nullptr;
        }
       
     TreeNode *root = new TreeNode();
        root->val=porder[0];
        if (len1 ==1 || len2 ==1) {
            return root;
        }
        int r=0;
        for (int i = 0; i < len1 ; ++i) {
            if (iorder[i] == root->val) {
                r = i;
                break;
            }
        }
        vector<int> linorder;
        vector<int> rinorder;
        vector<int> rporder;
        vector<int> lporder;
         for (int i = 0; i < r ; ++i) {
             linorder.push_back(iorder[i]);
         }
         for (int i = r+1; i < len2 ; ++i) {
             rinorder.push_back(iorder[i]);
         }
          for (int i = 1; i <= r ; ++i) {
             lporder.push_back(porder[i]);
         }
          for (int i = r+1; i <= len1; ++i) {
             rporder.push_back(porder[i]);
         }
        
         root->left =dp(lporder,linorder);
         root->right =dp(rporder,rinorder);
         return root;
    }
};


Lintcode前序遍历和中序遍历树构造二叉树

标签:

原文地址:http://blog.csdn.net/susser43/article/details/46491907

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!