• leetcode-105 从前序与中序遍历序列构造二叉树-使用栈代替递归


    使用递归法这个是很容易实现的(当然前提是你对二叉树遍历顺序熟悉)
    本文重点是采用栈的方式来解决问题, 毕竟据说递归能实现的解法用栈都能实现

    步骤一: 观察递归法需要知道的状态

    一般常见写法

    这个写法可能没那么好观察出状态

    class Solution {
    public:
        TreeNode* buildTreeHelper(vector<int>& preorder, int l1, int h1, vector<int>& inorder, int l2, int h2) {
            if (l1 > h1) {
                return nullptr;
            }
            int val = preorder[l1];
            int i = l2;
            for (; i <= h2; i++) {
                if (inorder[i] == val) {
                    break;
                }
            }
            int lsz = i - l2;
            auto root = new TreeNode(val);
            root->left = buildTreeHelper(preorder, l1 + 1, l1 + lsz, inorder, l2, i - 1);
            root->right= buildTreeHelper(preorder, l1 + lsz + 1, h1, inorder, i + 1, h2);
            return root;
        }
    
        // 最一般的递归构造
        TreeNode* buildTree_recursion(vector<int>& preorder, vector<int>& inorder) {
            return buildTreeHelper(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    那么如果换成下面的写法

       void buildTreeHelper(vector<int>& preorder, int l1, int h1, vector<int>& inorder, int l2, int h2, TreeNode* &father, TreeNode* TreeNode::*child) {
            if (l1 > h1) {
                return;
            }
            int val = preorder[l1];
            auto node = new TreeNode(val);
            if (nullptr == father) {
                father = node;
            } else {
                father->*child = node;
            }
            int i = l2;
            for (; i <= h2; i++) {
                if (inorder[i] == val) {
                    break;
                }
            }
            int lsz = i - l2;
            buildTreeHelper(preorder, l1 + 1, l1 + lsz, inorder, l2, i - 1, node, &TreeNode::left);
            buildTreeHelper(preorder, l1 + lsz + 1, h1, inorder, i + 1, h2, node, &TreeNode::right);
        }
    
        // 最一般的递归构造
        TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
            TreeNode* root = nullptr;
            buildTreeHelper2(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1, root, nullptr);
            return root;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    知道栈必须保存的状态

    1. preorder 全局可以获取
    2. l1
    3. h1
    4. inorder 全局可以获取
    4. l2
    5. h2
    6. father
    7. child 或者用tag代替

    定义栈保存结构

    可用结构一

      struct OneItemTag {
            int l1, h1, l2, h2;
            int tag;
            TreeNode* father;
            OneItemTag(int l1, int h1, int l2, int h2, int tag, TreeNode* fa = nullptr):l1(l1), h1(h1), l2(l2), h2(h2), tag(tag), father(fa){}
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    可用结构二

      struct OneItem {
            int l1, h1, l2, h2;
            TreeNode* TreeNode::*child;
            TreeNode* father;
            OneItem(int l1, int h1, int l2, int h2, TreeNode* TreeNode:: *child, TreeNode* fa = nullptr):l1(l1), h1(h1), l2(l2), h2(h2), child(child), father(fa){}
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    逻辑处理

    /*
     * @lc app=leetcode.cn id=105 lang=cpp
     *
     * [105] 从前序与中序遍历序列构造二叉树
     */
    
    // @lc code=start
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        TreeNode* buildTreeHelper(vector<int>& preorder, int l1, int h1, vector<int>& inorder, int l2, int h2) {
            if (l1 > h1) {
                return nullptr;
            }
            int val = preorder[l1];
            int i = l2;
            for (; i <= h2; i++) {
                if (inorder[i] == val) {
                    break;
                }
            }
            int lsz = i - l2;
            auto root = new TreeNode(val);
            root->left = buildTreeHelper(preorder, l1 + 1, l1 + lsz, inorder, l2, i - 1);
            root->right= buildTreeHelper(preorder, l1 + lsz + 1, h1, inorder, i + 1, h2);
            return root;
        }
    
        // 最一般的递归构造
        TreeNode* buildTree_recursion(vector<int>& preorder, vector<int>& inorder) {
            return buildTreeHelper(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
        }
    
        struct OneItemTag {
            int l1, h1, l2, h2;
            int tag;
            TreeNode* father;
            OneItemTag(int l1, int h1, int l2, int h2, int tag, TreeNode* fa = nullptr):l1(l1), h1(h1), l2(l2), h2(h2), tag(tag), father(fa){}
        };
    
        TreeNode* buildTreeTag(vector<int>& preorder, vector<int>& inorder) {
            if (preorder.empty()) {
                return nullptr;
            }
            stack<OneItemTag> st;
            TreeNode* root = nullptr;
            st.push(OneItemTag(0, (int)preorder.size() - 1, 0, (int)inorder.size() - 1, 0, nullptr));
    
            while (!st.empty()) {
                auto one = st.top();
                st.pop();
                int l1 = one.l1;
                int h1 = one.h1;
                int l2 = one.l2;
                int h2 = one.h2;
    
                int tag = one.tag;
                TreeNode* father = one.father;
                if (l1 > h1) {
                    continue;
                }
                if (l1 == h1) {
                    auto node = new TreeNode(preorder[l1]);
                    if (father == nullptr) {
                        root = node;
                    } else {
                        if (-1 == tag) {
                            father->left = node;
                        } else if (1 == tag) {
                            father->right = node;
                        }
                    }
                    continue;
                }
                int val = preorder[l1];
                int i = l2;
                for (; i <= h2; i++) {
                    if (inorder[i] == val) {
                        break;
                    }
                }
                int lsz = i - l2;
                auto node = new TreeNode(val);
                if (father == nullptr) {
                    root = node;
                } else {
                    if (-1 == tag) {
                        father->left = node;
                    } else if (1 == tag) {
                        father->right = node;
                    }
                }
                st.push(OneItemTag(l1 + 1, l1 + lsz, l2, i - 1, -1, node));
                st.push(OneItemTag(l1 + lsz + 1, h1, i + 1, h2, 1, node));
            }
            return root;
        }
    
        struct OneItem {
            int l1, h1, l2, h2;
            TreeNode* TreeNode::*child;
            TreeNode* father;
            OneItem(int l1, int h1, int l2, int h2, TreeNode* TreeNode:: *child, TreeNode* fa = nullptr):l1(l1), h1(h1), l2(l2), h2(h2), child(child), father(fa){}
        };
        // 递归的都能用栈, 用栈场所
        TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
            if (preorder.empty()) {
                return nullptr;
            }
            stack<OneItem> st;
            TreeNode* root = nullptr;
            st.push(OneItem(0, (int)preorder.size() - 1, 0, (int)inorder.size() - 1, nullptr, nullptr));
            while (!st.empty()) {
                auto one = st.top();
                st.pop();
                int l1 = one.l1;
                int h1 = one.h1;
                int l2 = one.l2;
                int h2 = one.h2;
                auto child = one.child;
                TreeNode* father = one.father;
                if (l1 > h1) {
                    continue;
                }
                if (l1 == h1) {
                    auto node = new TreeNode(preorder[l1]);
                    if (father == nullptr) {
                        root = node;
                    } else {
                        father->*child = node;
                    }
                    continue;
                }
                int val = preorder[l1];
                int i = l2;
                for (; i <= h2; i++) {
                    if (inorder[i] == val) {
                        break;
                    }
                }
                int lsz = i - l2;
                auto node = new TreeNode(val);
                if (father == nullptr) {
                    root = node;
                } else {
                    father->*child = node;
                }
                st.push(OneItem(l1 + 1, l1 + lsz, l2, i - 1, &TreeNode::left, node));
                st.push(OneItem(l1 + lsz + 1, h1, i + 1, h2, &TreeNode::right, node));
            }
            return root;
        }
    };
    // @lc code=end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164

    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树
    leetcode-105 从前序与中序遍历序列构造二叉树

  • 相关阅读:
    Qt调用sqlserver的存储过程
    区块链金融的开发流程
    网盘——文件重命名
    数据库复制:开源工具和选项
    js逆向学习笔记【一篇就够】
    The WebSocket session [x] has been closed and no method (apart from close())
    Vue3中rem适配方案 淘宝flexible在PC端适配
    Android性能分析工具-systrace使用
    商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    YAYA LIVE CTO 唐鸿斌:真正本地化,要让产品没有「产地」属性
  • 原文地址:https://blog.csdn.net/qq_34179431/article/details/126060394