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;
}