(二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals.

Values in the traversals pre and post are distinct positive integers.

Example 1:

Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]

Note:

  • 1 <= pre.length == post.length <= 30
  • pre[] and post[] are both permutations of 1, 2, ..., pre.length.
  • It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.

----------------------------------------------------------------------------------------------------------------------------------

这个是从先序遍历和后序遍历构建二叉树,不过却和之前的leetcode105 和 leetcode106的从中序遍历和先序遍历构建二叉树以及中序遍历和后序遍历构建二叉树不同的是,这个构建二叉树是不唯一的。另外,实现的代码和之前的两个题是也有一些不同。(注:关于这个怎么确定从先序遍历和后序遍历构建二叉树,可以看官方题解:https://leetcode.com/articles/construct-binary-tree-from-preorder-and-postorder-/  和另一个大佬的博客https://blog.csdn.net/waple_0820/article/details/81837875

C++代码1:

这个代码和leetcode 105 以及106的的用时20多ms的代码相比,添加了其中一个数组的长度(pre.size()),这样的会方便确定递归遍历时的终止点。不过有些代码是多余的。

/**
 * 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:
    TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
        return build(pre,0,pre.size()-1,post,0,pre.size() - 1,pre.size());
    }
    TreeNode* build(vector<int> &pre,int prel,int prer,vector<int> &post,int posl,int posr,int N){
        if(prel > prer || posl > posr) return NULL;
        if(N == 0)
            return NULL;
        TreeNode *cur = new TreeNode(pre[prel]);
        if(N == 1)
            return cur;
        int i = 1;
        for(i = 1; i < N; i++){
            if(pre[prel + 1] == post[posl + i - 1])
                break;
        }
        cur->left = build(pre,prel + 1,prel + i,post,posl,posl + i - 1,i);
        cur->right = build(pre,prel + i + 1,prer,post,posl + i,posr-1,N-1-i);
        return cur;
    }
};

C++代码2:

去除以上的多余的代码:build()里面的参数去掉了pre和post数组的末端的数的下标,不过保留了pre和post数组的长度,这个也会确定pre的末端的数的下标。

/**
 * 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:
    TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
        return build(pre,0,post,0,pre.size());
    }
    TreeNode* build(vector<int> &pre,int prel,vector<int> &post,int postl,int N){
        if(N == 0) return 0;
        TreeNode *cur = new TreeNode(pre[prel]);
        if(N == 1) return cur;
        int i = 1;
        for(i = 1; i < N; i++){
            if(pre[prel + 1] == post[postl + i - 1])
                break;
        }
        cur->left = build(pre,prel + 1,post,postl,i);
        cur->right = build(pre,prel + i + 1,post,postl + i,N - 1 - i);
        return cur;
    }
};

C++代码3:

和leetcode105和106的用时150ms以上的代码基本类似,就是 if(pre.size() == 1 || post.size() == 1) return cur;这个代码不能漏掉,这个是必须写上的递归的终止条件。还有,这个代码耗时比较少。。。。才20多ms。

/**
 * 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:
    TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
        return build(pre,post);
    }
    TreeNode* build(vector<int> &pre,vector<int> &post){
        if(pre.size() == 0 || post.size() == 0) return NULL;
        int rootval = pre[0];
        TreeNode *cur = new TreeNode(rootval);
        if(pre.size() == 1 || post.size() == 1) return cur;
        int i = 1;
        for(i = 1; i < pre.size(); i++){
            if(pre[1] == post[i - 1])
                break;
        }
        vector<int> prel,prer,postl,postr;
        for(int k = 1; k <= i; k++){
            prel.push_back(pre[k]);
        }
        for(int k = i + 1; k < pre.size(); k++){
            prer.push_back(pre[k]);
        }
        for(int k = 0; k < i; k++){
            postl.push_back(post[k]);
        }
        for(int k = i; k < post.size() - 1; k++){
            postr.push_back(post[k]);
        }
        cur->left = build(prel,postl);
        cur->right = build(prer,postr);
        return cur;
    }
};

原文地址:https://www.cnblogs.com/Weixu-Liu/p/10772892.html

时间: 2024-10-11 03:39:21

(二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal的相关文章

[LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals?pre?and?post?are distinct?positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

889.Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

leetcode题解: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. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树【Python】

LeetCode 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树[Medium][Python][二叉树][递归] Problem LeetCode Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  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. 代码: class Solution { public: TreeNode *buildTr

[LeetCode]*105.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. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t