zoukankan      html  css  js  c++  java
  • 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
    ",node->val);
                    if (st.top()->left == nullptr) {
                        st.top()->left = node;
                        printf("%d left child is %d
    ", st.top()->val, node->val);
                    } else {
                        st.top()->right = node;
                        printf("%d right child is %d
    ", st.top()->val, node->val);
                    }
                    j++;
                    printf("j: %d
    ",j);
                }
                if (i < pre.size()){
                    st.push(new TreeNode(pre[i]));
                    printf("push %d
    ",pre[i]);
                }
            }
    
            while (true) {
                node = st.top();
                st.pop();
                if(st.empty()) break;
                printf("pop %d
    ",node->val);
                if (st.top()->left == nullptr) {
                    st.top()->left = node;
                    printf("%d left child is %d
    ", st.top()->val, node->val);
                } else {
                    st.top()->right = node;
                    printf("%d right child is %d
    ", st.top()->val, node->val);
                }
                j++;
                printf("j: %d
    ",j);
            }
            return root;
    //        printf("return
    ");
    //        printf("root->value %d
    ",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 << "
    ";
        }
    }
    
    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
    ",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
    ",node->val);
                    if(st.empty())
                        return root;
                    if (st.top()->left == nullptr) {
                        st.top()->left = node;
                        //printf("%d left child is %d
    ", st.top()->val, node->val);
                    } else {
                        st.top()->right = node;
                        //printf("%d right child is %d
    ", st.top()->val, node->val);
                    }
                    j++;
                    //printf("j: %d
    ",j);
                }
                if (i < pre.size()){
                    st.push(new TreeNode(pre[i]));
                    //printf("push %d
    ",pre[i]);
                }
            }
        }
    };
  • 相关阅读:
    【sqli-labs】 less61 GET -Challenge -Double Query -5 queries allowed -Variation4 (GET型 挑战 双查询 只允许5次查询 变化4)
    Spring overview
    Code First use dotConnect for MySQL
    讓 MySQL 能夠用 EF6
    Sublime Text 3 常用插件以及安装方法(转)
    EntityFramework 6.0< Code First > 连接 Mysql数据库(转)
    bootstrap 2.3版与3.0版的使用区别
    用google-code-prettify高亮代码
    MVC中的@Html.DisplayFor等方法如何控制日期的显示格式(转)
    给Jquery easyui 的datagrid 每行增加操作链接(转)
  • 原文地址:https://www.cnblogs.com/learning-c/p/9847610.html
Copyright © 2011-2022 走看看