|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=106 lang=cpp |
| 3 | + * |
| 4 | + * [106] 从中序与后序遍历序列构造二叉树 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <vector> |
| 9 | +#include <queue> |
| 10 | +using namespace std; |
| 11 | +struct TreeNode |
| 12 | +{ |
| 13 | + int val; |
| 14 | + TreeNode *left; |
| 15 | + TreeNode *right; |
| 16 | + TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 17 | + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 18 | + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 19 | +}; |
| 20 | + |
| 21 | +// @lc code=start |
| 22 | +/** |
| 23 | + * Definition for a binary tree node. |
| 24 | + * struct TreeNode { |
| 25 | + * int val; |
| 26 | + * TreeNode *left; |
| 27 | + * TreeNode *right; |
| 28 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 29 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 30 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 31 | + * }; |
| 32 | + */ |
| 33 | +class Solution |
| 34 | +{ |
| 35 | +public: |
| 36 | + TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) |
| 37 | + { |
| 38 | + return build(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); |
| 39 | + } |
| 40 | + TreeNode *build(vector<int> &inorder, int inStart, int inEnd, vector<int> &postorder, int postStart, int postEnd) |
| 41 | + { |
| 42 | + if (inStart > inEnd) |
| 43 | + return NULL; |
| 44 | + auto root = new TreeNode(postorder[postEnd]); |
| 45 | + // 从中序遍历找到根 |
| 46 | + int idx = inStart; |
| 47 | + for (; idx <= inEnd; idx++) |
| 48 | + { |
| 49 | + if (inorder[idx] == postorder[postEnd]) |
| 50 | + { |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + int leftSize = idx - inStart; |
| 55 | + // 左子树 |
| 56 | + root->left = build(inorder, inStart, idx - 1, postorder, postStart, postStart + leftSize - 1); |
| 57 | + root->right = build(inorder, idx + 1, inEnd, postorder, postStart + leftSize, postEnd - 1); |
| 58 | + return root; |
| 59 | + } |
| 60 | +}; |
| 61 | +// @lc code=end |
| 62 | + |
| 63 | +int main() |
| 64 | +{ |
| 65 | + Solution s; |
| 66 | + vector<int> pre = {9, 3, 15, 20, 7}; |
| 67 | + vector<int> in = {9, 15, 7, 20, 3}; |
| 68 | + TreeNode *root = s.buildTree(pre, in); |
| 69 | + // 层序遍历 |
| 70 | + queue<TreeNode *> q; |
| 71 | + q.push(root); |
| 72 | + // auto a = new TreeNode(9); |
| 73 | + // cout << a->val; |
| 74 | + // cout << a->val; |
| 75 | + cout << '['; |
| 76 | + while (!q.empty()) |
| 77 | + { |
| 78 | + TreeNode *now = q.front(); |
| 79 | + cout << now->val; |
| 80 | + q.pop(); |
| 81 | + if (now->left != nullptr) |
| 82 | + { |
| 83 | + q.push(now->left); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + cout << "null"; |
| 88 | + } |
| 89 | + if (now->right != nullptr) |
| 90 | + { |
| 91 | + q.push(now->right); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + cout << "null"; |
| 96 | + } |
| 97 | + cout << ","; |
| 98 | + } |
| 99 | + cout << "]"; |
| 100 | +} |
0 commit comments