Construct Binary Tree系列

Construct Binary Tree from Preorder and Inorder Traversal

 

 

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

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

 

知道先序序列和中序序列,构建二叉树。
假设 先序序列为   a  b  c  d  e  h  f
        中序序列为   b  c  a  e  h  d  f
1)首先可以得到a为根节点,在中序序列中bc在a的左边,ehdf在a的右边,故有
                                  
2)此时左子树  先序序列为   b  c
                          中序序列为   b  c
     故b为根,可得
                                  
3)b的左子树为空,右子树只有c,这时a的左子树就构建完毕
                                  
4)在看a的右子树的  先序序列    d  e  h  f
                                   中序序列    e  h  d  f
      直接确定d为根节点,左子树节点为eh,右子树节点为f
                                 
5)重复上面的方法,最后可得二叉树为
                                  
由此,可以写出根据先序序列和中序序列构建二叉树的递归方法,代码如下:
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        TreeNode* root = NULL;
        constructTree(preorder, inorder, 0, 0, preorder.size(), root);
        return root;
    }
    
    void constructTree(vector<int>& preorder, vector<int>& inorder, int s1, int s2, int len, TreeNode*& root) {
        if( len < 1 )
            return ;
        root = new TreeNode( preorder[s1] );        //建立根节点
        vector<int>::iterator iter = find( inorder.begin()+s2, inorder.begin()+s2+len, root->val );     //在中序序列中找到根节点位置
        int leftnum = iter - ( inorder.begin() + s2 );      //左子树节点个数
        int rightnum = len - leftnum - 1;                   //右子树节点个数
        constructTree( preorder, inorder, s1+1, s2, leftnum, root->left );  //构建左子树
        constructTree( preorder, inorder, s1+leftnum+1, s2+leftnum+1, rightnum, root->right);   //构建右子树
    }
};

Construct Binary Tree from Inorder and Postorder Traversal

 

 

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

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

 

根据中序序列和后序序列,构建二叉树,方法和上相同,同样以递归方法构建,代码如下:
 
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        TreeNode* root = NULL;
        constructTree(inorder, postorder, 0, 0, inorder.size(), root);
        return root;
    }
    
    void constructTree(vector<int>& inorder, vector<int>& postorder, int s1, int s2, int len, TreeNode*& root) {
        if( len < 1 )
            return ;
        int rootIndex = s2 + len - 1;       //后序序列中,最后一个元素是根元素
        root = new TreeNode( postorder[rootIndex] );
        //在中序序列中找到根元素的位置
        vector<int>::iterator iter = find( inorder.begin() + s1, inorder.begin() + s1 + len, postorder[rootIndex] );
        int leftNum = iter - (inorder.begin() + s1);    //计算左子树节点的数目
        int rightNum = len - leftNum - 1;       //计算右子树节点的数目
        constructTree( inorder, postorder, s1, s2, leftNum, root->left);    //构建左子树
        constructTree( inorder, postorder, s1+leftNum+1, s2+leftNum, rightNum, root->right);    //构建右子树
    }
};


 

posted on 2014-08-12 23:38  bug睡的略爽  阅读(209)  评论(0编辑  收藏  举报

导航