zoukankan      html  css  js  c++  java
  • Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder 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.

    这道题之前算法课上好像遇到过,思路也很简单的。

    思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可。(在去除了根节点之后,中序遍历和后序遍历的前N个树都是左子树,有了这个前提之后,代码也就好写了。)

    特别注意的是:之前提到过,每当涉及到树,就应该考虑到递归能不能用。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
           typedef vector<int>::iterator Iter;
           TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
               // IMPORTANT: Please reset any member data you declared, as
               // the same Solution instance will be reused for each test case.
               return buildTreeRecur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
           }
           TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
           {
               if(istart == iend)return NULL;
               int rootval = *(pend-1);
               Iter iterroot = find(istart, iend, rootval);
               TreeNode *res = new TreeNode(rootval);
               res->left = buildTreeRecur(istart, iterroot, pstart, pstart+(iterroot-istart));
               res->right = buildTreeRecur(iterroot+1, iend, pstart+(iterroot-istart), pend-1);
               return res;
        }
    };

     

    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.

      同上,只是树根是先序序列的第一个元素

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
         typedef vector<int>::iterator Iter;
         TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
             // IMPORTANT: Please reset any member data you declared, as
             // the same Solution instance will be reused for each test case.
             return buildTreeRecur(inorder.begin(), inorder.end(), preorder.begin(), preorder.end());
         }
         TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
         {
             if(istart == iend)return NULL;
             int rootval = *pstart;
             Iter iterroot = find(istart, iend, rootval);
             TreeNode *res = new TreeNode(rootval);
             res->left = buildTreeRecur(istart, iterroot, pstart+1, pstart+1+(iterroot-istart));
             res->right = buildTreeRecur(iterroot+1, iend, pstart+1+(iterroot-istart), pend);
             return res;
         }
    };

      

  • 相关阅读:
    LAB8 android
    python3速查参考- python基础 1 -> python版本选择+第一个小程序
    selenium三种断言以及异常类型
    selenium+java:获取列表中的值
    selenium测试(Java)--元素操作(五)
    redhat网卡设置
    PYTHON TDD学习(一)-->Python 3.4版本环境安装Django及其启动
    渗透测试
    实用--功能测试方法与实际测试内容
    SQL常见面试题(学生表_课程表_总表)
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4761722.html
Copyright © 2011-2022 走看看