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.

Runtime: 8 ms, faster than 98.12% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.

#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include <iostream>

using namespace std;

//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) {
        if (pre.size() == 0) return nullptr;
        stack<TreeNode *> st;
        TreeNode *root = new TreeNode(pre[0]);
        st.push(root);
        int j = 0;
        int i = 0;
        TreeNode *node = nullptr;
        for(int i=1;i<pre.size();++i){
            while (st.top()->val == post[j]) {
                node = st.top();
                st.pop();
                printf("pop %d\n",node->val);
                if (st.top()->left == nullptr) {
                    st.top()->left = node;
                    printf("%d left child is %d\n", st.top()->val, node->val);
                } else {
                    st.top()->right = node;
                    printf("%d right child is %d\n", st.top()->val, node->val);
                }
                j++;
                printf("j: %d\n",j);
            }
            if (i < pre.size()){
                st.push(new TreeNode(pre[i]));
                printf("push %d\n",pre[i]);
            }
        }

        while (true) {
            node = st.top();
            st.pop();
            if(st.empty()) break;
            printf("pop %d\n",node->val);
            if (st.top()->left == nullptr) {
                st.top()->left = node;
                printf("%d left child is %d\n", st.top()->val, node->val);
            } else {
                st.top()->right = node;
                printf("%d right child is %d\n", st.top()->val, node->val);
            }
            j++;
            printf("j: %d\n",j);
        }
        return root;
//        printf("return\n");
//        printf("root->value %d\n",root->val);
//        return root;
    }
};

void show_tree(TreeNode *root) {
    if (root == nullptr) {
        cout<<"root is nullptr"<<endl;
        return;
    }
    cout<<"root is not nullptr"<<endl;
    queue<TreeNode *> qu;
    qu.push(root);
    int sz;
    while (!qu.empty()) {
        sz = qu.size();
        TreeNode* node= nullptr;
        for (int i = 0; i < sz; ++i) {
            node=qu.front();
            cout << node->val << " ";
            qu.pop();
            if (node->left)
                qu.push(node->left);
            if (node->right)
                qu.push(node->right);
        }
        cout << "\n";
    }
}

int main() {
    vector<int> pre{1, 2, 4, 5, 3, 6, 7};
    vector<int> post{4, 5, 2, 6, 7, 3, 1};
    Solution solution;
    TreeNode *res = solution.constructFromPrePost(pre, post);

//    solution.constructFromPrePost(pre, post);
    printf("res value %d\n",res->val);
    show_tree(res);
    return 0;

}

提交代码

class Solution {
public:
    TreeNode *constructFromPrePost(vector<int> &pre, vector<int> &post) {
        if (pre.size() == 0) return nullptr;
        stack<TreeNode *> st;
        TreeNode *root = new TreeNode(pre[0]);
        st.push(root);
        int j = 0;
        TreeNode *node = nullptr;
        for(int i=1;i<=pre.size();++i){
            while (st.top()->val == post[j]) {
                node = st.top();
                st.pop();
                //printf("pop %d\n",node->val);
                if(st.empty())
                    return root;
                if (st.top()->left == nullptr) {
                    st.top()->left = node;
                    //printf("%d left child is %d\n", st.top()->val, node->val);
                } else {
                    st.top()->right = node;
                    //printf("%d right child is %d\n", st.top()->val, node->val);
                }
                j++;
                //printf("j: %d\n",j);
            }
            if (i < pre.size()){
                st.push(new TreeNode(pre[i]));
                //printf("push %d\n",pre[i]);
            }
        }
    }
};

原文地址:https://www.cnblogs.com/learning-c/p/9847610.html

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

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 <=

(二叉树 递归) 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 <=

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu

[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笔记: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. 二. 题目分析 这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树,然后遍历右子树,最

Construct Binary Tree from Inorder and Postorder Traversal (算法课上的题)

Construct Binary Tree from Inorder and Postorder Traversal 这道题之前算法课上好像遇到过,思路也很简单的. 思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可.(在去除了根节点之后,中序遍历和后序遍历的前N个树都是

[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. 这道题要求用先序和中序遍历来建立二叉树,跟之前那道Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树原理基本相同,针对这道题,由于先

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;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[Tree]: 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. 这个题目的解法与LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal的解法几乎相差无几.我的C++代码实现如下: class S