当前位置:   article > 正文

从前序遍历序列恢复BST_前序还原bst

前序还原bst

相关问题:已知二叉树的前序遍历序列和中序遍历序列,要求重新恢复该二叉树。

给你一个二叉搜索树的前序遍历序列,恢复此二叉搜索树

文章末尾给出了一个复杂度是O(n)的算法。复杂度是O(n^2)的代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4. using namespace std;
  5. struct Node
  6. {
  7. int key; Node* left; Node* right;
  8. Node(int k, Node* l, Node* r): key(k), left(l), right(r){};
  9. };
  10. Node* construct(vector<int> pre)
  11. {
  12. if(pre.size())
  13. {
  14. int data = pre[0];
  15. vector<int> pre_l, pre_r;
  16. if(pre.size()>1)
  17. for(int i=1; i<pre.size(); i++)
  18. if(pre[i]<pre[0])
  19. pre_l.push_back(pre[i]);
  20. else
  21. pre_r.push_back(pre[i]);
  22. Node* root = new Node(data, NULL, NULL);
  23. root->left = construct(pre_l);
  24. root->right = construct(pre_r);
  25. return root;
  26. }
  27. else
  28. return NULL;
  29. }
  30. void print(Node* root){
  31. if(root){
  32. print(root->left); printf("%d|", root->key);
  33. print(root->right);
  34. }
  35. }
  36. int main()
  37. {
  38. int pre[] = {10, 5, 1, 7, 40, 50};
  39. vector<int> pre_vec = vector<int>(pre, pre+6);
  40. Node* root = construct(pre_vec);
  41. print(root);
  42. }

复杂度是O(n)的算法。思路:

 The trick is to set a range {min.. max} for every node. Initialize the range as {INT_MIN .. INT_MAX}.The first node will definitely be in range, so create root node. Toconstruct the left subtree, set the range as {INT_MIN …root->data}.If a values is in the range {INT_MIN .. root->data}, the values ispart part of left subtree. To construct the right subtree, set therange as {root->data..max .. INT_MAX}.

以下是网上某人的代码。我看不明白为何constructTreeUtil 的参数列表里 int key 的作用。

  1. // A recursive function to construct BST from pre[]. preIndex is used
  2. // to keep track of index in pre[].
  3. Node* constructTreeUtil( int pre[], int* preIndex, int key,
  4. int min, int max, int size ){
  5. // Base case
  6. if( *preIndex >= size )
  7. return NULL;
  8. Node* root = NULL;
  9. // If current element of pre[] is in range, then
  10. // only it is part of current subtree
  11. if( key > min && key < max ) {
  12. root = new Node (key, NULL, NULL );
  13. *preIndex = *preIndex + 1;
  14. if (*preIndex < size) {
  15. root->left = constructTreeUtil( pre, preIndex, pre[*preIndex],
  16. min, key, size );
  17. root->right = constructTreeUtil( pre, preIndex, pre[*preIndex],
  18. key, max, size );
  19. }
  20. }
  21. return root;
  22. }
  23. // The function to construct BST from given preorder traversal.
  24. Node *constructTree (int pre[], int size)
  25. {
  26. int preIndex = 0;
  27. return constructTreeUtil ( pre, &preIndex, pre[0], INT_MIN, INT_MAX, size );
  28. }
  29. int main()
  30. {
  31. int pre[] = {10, 5, 1, 7, 40, 50};
  32. Node* root = constructTree(pre, 6);
  33. print(root);
  34. }

我把constructTreeUtil 的参数 int key 去掉之后,新的代码代码如下(以下代码经过测试):

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <iostream>
  4. struct Node
  5. {
  6. int key; Node* left; Node* right;
  7. Node(int k, Node* l, Node* r): key(k), left(l), right(r){};
  8. };
  9. // A recursive function to construct BST from pre[]. preIndex is used
  10. // to keep track of index in pre[].
  11. Node* constructTreeUtil( int pre[], int* preIndex,
  12. int min, int max, int size ){
  13. // Base case
  14. if( *preIndex >= size )
  15. return NULL;
  16. Node* root = NULL;
  17. int key = pre[*preIndex];
  18. // If current element of pre[] is in range, then
  19. // only it is part of current subtree
  20. if( key > min && key < max ) {
  21. root = new Node (key, NULL, NULL );
  22. *preIndex = *preIndex + 1;
  23. if (*preIndex < size) {
  24. root->left = constructTreeUtil( pre, preIndex,
  25. min, key, size );
  26. root->right = constructTreeUtil( pre, preIndex,
  27. key, max, size );
  28. }
  29. }
  30. return root;
  31. }
  32. // The function to construct BST from given preorder traversal.
  33. Node *constructTree (int pre[], int size)
  34. {
  35. int preIndex = 0;
  36. return constructTreeUtil ( pre, &preIndex, INT_MIN, INT_MAX, size );
  37. }
  38. void print(Node* root){
  39. if(root){
  40. print(root->left); printf("%d|", root->key);
  41. print(root->right);
  42. }
  43. }
  44. int main()
  45. {
  46. int pre[] = {10, 8, 7, 6, 9, 100, 20, 30, 40, 35, 110};
  47. Node* root = constructTree(pre, 11);
  48. print(root);
  49. int x; std::cin>>x;
  50. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/274955
推荐阅读
相关标签
  

闽ICP备14008679号