用 C++ 平衡二叉查找树


假设我们有一个二叉查找树,我们必须找到一个具有相同节点值的平衡二叉查找树。当且仅当每个节点的两个子树的深度仅差 1 时,才称二叉查找树为平衡的。如果有多个结果,则返回任何一个结果。如果树像这样:-

为了解决此问题,我们将按照以下步骤操作:-

  • 定义 inorder() 方法,它将按顺序遍历序列存储到数组中

  • 定义 construct 方法(),它将采用 low 和 high -

  • 如果 low > high,则返回 null

  • mid := low + (high - low) / 2

  • root := 带有值 arr[mid] 的新节点

  • root 的左侧 := construct(low, mid – 1),root 的右侧 := construct(mid + 1, high)

  • 返回 root

  • 从 main 方法调用 inorder 方法并返回 construct(0, arr - 1 的大小)

示例 (C++)

让我们看看以下实现以更好地理解 -

 在线演示

#include <bits/stdc++.h>
using namespace std;
class TreeNode{
   public:
   int val;
   TreeNode *left, *right;
   TreeNode(int data){
      val = data;
      left = right = NULL;
   }
};
void insert(TreeNode **root, int val){
   queue<TreeNode*> q;
   q.push(*root);
   while(q.size()){
      TreeNode *temp = q.front();
      q.pop();
      if(!temp->left){
         if(val != NULL)
            temp->left = new TreeNode(val);
         else
            temp->left = new TreeNode(0);
         return;
      }else{
         q.push(temp->left);
      }
      if(!temp->right){
      if(val != NULL)
         temp->right = new TreeNode(val);
      else
         temp->right = new TreeNode(0);
      return;
      }else{
         q.push(temp->right);
      }
   }
}
TreeNode *make_tree(vector<int> v){
   TreeNode *root = new TreeNode(v[0]);
   for(int i = 1; i<v.size(); i++){
      insert(&root, v[i]);
   }
   return root;
}
void tree_level_trav(TreeNode*root){
   if (root == NULL) return;
      cout << "[";
      queue<TreeNode *> q;
      TreeNode *curr;
      q.push(root);
      q.push(NULL);
      while (q.size() > 1) {
         curr = q.front();
         q.pop();
         if (curr == NULL){
            q.push(NULL);
      } else {
         if(curr->left)
            q.push(curr->left);
         if(curr->right)
            q.push(curr->right);
         if(curr->val == 0 || curr == NULL){
            cout << "null" << ", ";
         }else{
            cout << curr->val << ", ";
         }
      }
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector <int> arr;
   void inorder(TreeNode* node){
      if(!node || node->val == 0) return;
      inorder(node->left);
      arr.push_back(node->val);
      inorder(node->right);
   }
   TreeNode* construct(int low, int high){
      if(low > high) return NULL;
      int mid = low + (high - low) / 2;
      TreeNode* root = new TreeNode(arr[mid]);
      root->left = construct(low, mid - 1);
      root->right = construct(mid + 1, high);
      return root;
   }
   TreeNode* balanceBST(TreeNode* root) {
      inorder(root);
      return construct(0, (int)arr.size() - 1);
   }
};
main(){
   vector<int> v = {1,NULL,2,NULL,NULL,NULL,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4};
   TreeNode *root = make_tree(v);
   Solution ob;
   tree_level_trav(ob.balanceBST(root));
}

输入

[1,NULL,2,NULL,NULL,NULL,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4]

输出

[2, 1, 3, 4, ]

更新于: 2020 年 4 月 29 日

3K+ 次查看

启动你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.