zoukankan      html  css  js  c++  java
  • PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree

    题意:

    在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性。图1-4说明了旋转规则。

    现在给出一系列插入,
    您应该输出生成的AVL树的级别遍历序列,并告知它是否是完整的二叉树。

    输入规格:

    每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(<= 20)。
    一行中的所有数字都以空格分隔。

    输出规格:

    对于每个测试用例,将键逐个插入到初始空的AVL树中。然后首先在一行中打印生成的AVL树的级别遍历序列。
    并且行尾没有额外的空间。然后在下一行中,如果树完成,打印“是”,否则打印“否”。

    思路:

    AVL.上次做过一道关于AVL的要会写AVL旋转的模板就好了。最后输出是否是平衡二叉树就ok。

    ac代码:

    C++

    // pat1123.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<stdio.h>
    #include<map>
    #include<cmath>
    #include<unordered_map>
    #include<unordered_set>
    
    using namespace std;
    
    struct TreeNode
    {
    	int val;
    	TreeNode* left;
    	TreeNode* right;
    	int height;
    	TreeNode(int x) : val(x) , left(NULL) , right(NULL) , height(0) {}
    };
    
    int count_height(TreeNode*& node)
    {
    	if (!node) return -1;
    	else return node->height;
    }
    
    void LL(TreeNode* &root)
    {
    	TreeNode* temp = root->left;
    	root->left = temp->right;
    	temp->right = root;
    
    	root->height = max(count_height(root->right), count_height(root->left)) + 1;
    	temp->height = max(count_height(temp->right), count_height(temp->left)) + 1;
    
    	root = temp;
    }
    
    void RR(TreeNode* &root)
    {
    	TreeNode* temp = root->right;
    	root->right = temp->left;
    	temp->left = root;
    
    	root->height = max(count_height(root->right), count_height(root->left)) + 1;
    	temp->height = max(count_height(temp->right), count_height(temp->left)) + 1;
    
    	root = temp;
    }
    
    void RL(TreeNode* &root)
    {
    	LL(root->right);
    	RR(root);
    }
    
    void LR(TreeNode* &root)
    {
    	RR(root->left);
    	LL(root);
    }
    
    void insert(int val,TreeNode* &root)
    {
    	if (!root)
    	{
    		root = new TreeNode(val);
    		return;
    	}
    
    	if (root->val < val)
    	{
    		insert(val, root->right);
    		if (count_height(root->right) - count_height(root->left) > 1)
    		{
    			if (val > root->right->val) RR(root);
    			else RL(root);
    		}
    	}
    	else
    	{
    		insert(val, root->left);
    		if (count_height(root->left) - count_height(root->right) > 1)
    		{
    			if (val > root->left->val) LR(root);
    			else LL(root);
    		}
    	}
    
    	root->height = max(count_height(root->left), count_height(root->right)) + 1;
    }
    
    int main()
    {
    	int n,val;
    	scanf("%d", &n);
    	TreeNode* root = NULL;
    	for (int i = 0; i < n; i++)
    	{
    		scanf("%d", &val);
    		insert(val, root);
    	}
    
    	//level order traversal
    	queue<TreeNode*> q;
    	q.push(root);
    	vector<TreeNode*> level_order(1,NULL);
    	while (!q.empty())
    	{
    		TreeNode* top = q.front();
    		q.pop();
    		level_order.push_back(top);
    		if (top->left) q.push(top->left);
    		if (top->right) q.push(top->right);
    	}
    	for (int i = 1; i < n; i++)
    		printf("%d ", level_order[i]->val);
    	printf("%d
    ", level_order[n]->val);
    
    	//is complete
    	bool flag = true;
    	for (int i = 1; i <= n; i++)
    	{
    		if (2 * i <= n && (!level_order[i]->left || level_order[i]->left->val != level_order[2 * i]->val))
    		{
    			flag = false;
    			break;
    		}
    		else if(2 * i  + 1 <= n && (!level_order[i]->right || level_order[i]->right->val != level_order[2 * i + 1]->val))
    		{
    			flag = false;
    			break;
    		}
    	}
    	if (flag) printf("YES
    ");
    	else printf("NO
    ");
    
        return 0;
    }
    
    
    
  • 相关阅读:
    Laravel框架中的event事件操作
    PHP魔术方法实例
    PHP 面向对象
    ThinkPHP中where()使用方法详解
    PHP常见错误提示含义解释
    php面向对象编程self和static的区别
    php文件路径获取文件名
    php三种无限分类
    php高精度计算问题
    转:JavaScript定时机制、以及浏览器渲染机制 浅谈
  • 原文地址:https://www.cnblogs.com/weedboy/p/7352797.html
Copyright © 2011-2022 走看看