zoukankan      html  css  js  c++  java
  • 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description:

        Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree.

    Input:

        105. Preorder & Inorder traversal

        106. Inorder & Postorder traversal

    output:

        A binary tree.

    Solution:

      This solution uses the algorithm "divide and conquer". Taking an example of 105, we can get the root node from preorder travelsal then use inorder traversal to divide the range of left tree nodes and the right tree nodes. You can

    draw some simple instances on paper,  which will make you totally clear about the thinking. 

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    //105
    class Solution {
    public:
        void travel(TreeNode **root)
        {
            if(*root){
                travel(& ((*root) -> left));
                cout<<"val is "<<(*root)->val<<endl;
                travel(& ((*root) -> right));
            }
        }
    
        TreeNode* createTree(vector<int>& preorder, vector<int>& inorder, int preSta, int preEnd, int inSta, int inEnd)
        {
            if(preSta > preEnd || inSta > inEnd ) return NULL;
            TreeNode *root = new TreeNode(preorder[preSta]);
            int index;
            for(int i = inSta; i <= inEnd; i ++){
                if(inorder[i] == preorder[preSta]){
                    index = i;
                    break;
                }
            }
            root -> left = createTree(preorder, inorder, preSta + 1, preSta + index - inSta, inSta, index - 1);
            root -> right = createTree(preorder, inorder, preSta + index - inSta + 1, preEnd, index + 1, inEnd);
            return root;
        }
    
        TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
            TreeNode *root = createTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
            TreeNode **r = &root;
            //travel(r);
            return root;
        }
    };
    
    //106.
    class Solution {
    public:
        TreeNode* createTree(vector<int>& inorder, vector<int>& postorder, int inSta, int inEnd, int postSta, int postEnd){
            if(inSta > inEnd || postSta > postEnd)
                return NULL;
            TreeNode* root = new TreeNode(postorder[postEnd]);
            int index;
            for(int i = inEnd; i >= inSta; i --){
                if(postorder[postEnd] == inorder[i]){
                    index = i;
                    break;
                }
            }
            root -> right = createTree(inorder, postorder, index + 1, inEnd, postEnd - (inEnd - index), postEnd - 1);
            root -> left = createTree(inorder, postorder, inSta, index - 1, postSta, postSta + (index - inSta - 1));
            return root;
        }
        TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
            TreeNode* root = createTree(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
            return root;
        }
    };
  • 相关阅读:
    现实世界的Windows Azure:采访Definition 6首席技术官Paul Hernacki
    CloudConnect回顾,以及数据的未来
    现已可用:集成了Web Deploy的Windows Azure SDK 1.4更新版
    使用Azure SDK 1.4.1中的Web Deploy
    现实世界的SQL Azure:采访Zitec公司CEO,Alexandru Lapusan
    Microsoft和Toyota宣布战略合作伙伴关系,联手构建基于Windows Azure的下一代远程通信系统
    Delphi 2007 的重构功能
    关于类的入门的例子(6): 类引用示例
    获取所有汉字与 Unicode 的对照表
    关于类的入门例子(4): property
  • 原文地址:https://www.cnblogs.com/luntai/p/5655571.html
Copyright © 2011-2022 走看看