20170513_请判断二叉树是不是平衡二叉树?

20170513_请判断二叉树是不是平衡二叉树?


方法1:

/*判断一颗二叉树是不是AVL树?
*/
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;

struct BiTreeNode
{
	int val;
	BiTreeNode *pLeft;
	BiTreeNode *pRight;
	BiTreeNode(int x):val(x),pLeft(nullptr),pRight(nullptr) {}
};
//创建二叉树
void CreatBiTree(BiTreeNode * &root)
{
	int ch;
	cin>>ch;
	getchar();
	if(ch == -1)
	{
		root=NULL;
	}
	else
	{
		root=new BiTreeNode(ch);
		CreatBiTree(root->pLeft);
		CreatBiTree(root->pRight);
	}
}
//前序遍历二叉树:借助栈stack
void PreOrder(BiTreeNode *root)
{
	if(root==nullptr)
		return;
	stack<BiTreeNode *> s;
	while(root != nullptr || !s.empty())
	{
		while(root != nullptr)
		{
			cout<<root->val<<" ";
			s.push(root);
			root=root->pLeft;
		}
		if(!s.empty())
		{
			root=s.top();
			s.pop();
			root=root->pRight;
		}
	}
}

class Solution
{
public:
	int maxDepth(BiTreeNode *root)//求出根节点的最大深度!
	{
		if(root==nullptr)
			return 0;
		else
			return max(maxDepth(root->pLeft), maxDepth(root->pRight))+1;
	}
	int minDepth(BiTreeNode *root)//求出根节点的最小深度!
	{
		if(root==nullptr)
			return 0;
		else
			return min(minDepth(root->pLeft), minDepth(root->pRight))+1;
	}
public:
	//求出根节点的最大深度、最小深度,则最大深度和最小深度之差就是树中任一子树的深度差的最大值!
    bool isBalanced(BiTreeNode *root)
	{
		return (maxDepth(root) - minDepth(root) <= 1 );
	}
};

int main()
{
	BiTreeNode *root;
	cout<<"依次输入二叉树的结点数据:"<<endl;		// 10 20 -1 40 -1 -1 30 -1 -1
	CreatBiTree(root);
	cout<<"二叉树建立完成."<<endl;
	cout<<"前序遍历二叉树."<<endl;
	PreOrder(root);
	cout<<endl;
	cout<<"前序遍历二叉树完成."<<endl;

	Solution example;
	cout<<"二叉树最大深度:"<<example.maxDepth(root)<<endl;
	cout<<"二叉树最小深度:"<<example.minDepth(root)<<endl;
	bool isblance=example.isBalanced(root);
	cout<<"输入的二叉树是平衡二叉树?"<<isblance<<endl;

	system("pause");
	return 0;
}



方法2:

///110. Balanced Binary Tree 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include<iostream>
#include<vector>
using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x):val(x),left(nullptr),right(nullptr) {}
};

void CreatBiTree(TreeNode * &root)
{
	int ch;
	cin>>ch;
	getchar();
	if(ch == -1)
		root=NULL;
	else
	{
		root=new TreeNode(ch);
		CreatBiTree(root->left);
		CreatBiTree(root->right);
	}
}

void PreOrder(TreeNode *root)
{
	TreeNode *s[100];
	int top=-1;
	while(root != nullptr || top != -1)
	{
		while(root != nullptr)
		{
			cout<<root->val<<" ";
			s[++top]=root;
			root=root->left;
		}
		if(top != -1)
		{
			root=s[top--];
			root=root->right;
		}
	}
}

class Solution
{
public:
	int maxDepth(TreeNode *root)//求出根节点的最大深度!
	{
		if(root==nullptr)
			return 0;
		else
			return max(maxDepth(root->left), maxDepth(root->right))+1;
	}
	int minDepth(TreeNode *root)//求出根节点的最小深度!
	{
		if(root==nullptr)
			return 0;
		else
			return min(minDepth(root->left), minDepth(root->right))+1;
	}
public:
	//求出根节点的最大深度、最小深度,则最大深度和最小深度之差就是树中任一子树的深度差的最大值!
    bool isBalanced(TreeNode *root)
	{
		return (maxDepth(root) - minDepth(root) <= 1 );
	}
};

int main()
{
	TreeNode *root;
	cout<<"依次输入二叉树的结点数据:"<<endl;
	CreatBiTree(root);
	cout<<"二叉树建立完成."<<endl;
	cout<<"前序遍历二叉树."<<endl;
	PreOrder(root);
	cout<<"前序遍历二叉树完成."<<endl;

	Solution example;
	cout<<"二叉树最大深度:"<<example.maxDepth(root)<<endl;
	cout<<"二叉树最小深度:"<<example.minDepth(root)<<endl;
	bool isblance=example.isBalanced(root);
	cout<<"输入的二叉树是平衡二叉树?"<<isblance<<endl;

	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值