判断一棵树是否为平衡二叉树
2015-05-27 21:57
337 查看
typedef char DataType;
typedef struct TNode
{
DataType value;
struct TNode* leftchild;
struct TNode* rightchild;
}TreeNode;
//求解树的高度
int TreeDepth(TreeNode* tree)
{
if(tree==NULL)
{
return 0;
}
int leftDepth = tree->leftchild;
int righDepth = tree->rightchild;
return (leftDepth>rightchild)?(leftDepth+1):(rightchild+1);
}
//判断一棵树是否为平衡二叉树
bool IsBalanced(TreeNode* tree)
{
if(tree==NULL)
{
return true;
}
int leftDepth = TreeDepth(tree->leftchild);
int rightDepth = TreeDepth(tree->rightchild);
int diff = leftDepth - rightDepth;
if(diff>1 || diff < -1)
{
return false;
}
return (IsBalanced(tree->leftchild)
&& IsBalanced(tree->rightchild));
}
typedef struct TNode
{
DataType value;
struct TNode* leftchild;
struct TNode* rightchild;
}TreeNode;
//求解树的高度
int TreeDepth(TreeNode* tree)
{
if(tree==NULL)
{
return 0;
}
int leftDepth = tree->leftchild;
int righDepth = tree->rightchild;
return (leftDepth>rightchild)?(leftDepth+1):(rightchild+1);
}
//判断一棵树是否为平衡二叉树
bool IsBalanced(TreeNode* tree)
{
if(tree==NULL)
{
return true;
}
int leftDepth = TreeDepth(tree->leftchild);
int rightDepth = TreeDepth(tree->rightchild);
int diff = leftDepth - rightDepth;
if(diff>1 || diff < -1)
{
return false;
}
return (IsBalanced(tree->leftchild)
&& IsBalanced(tree->rightchild));
}
相关文章推荐
- 如何判断一棵树是否是平衡二叉树
- 剑指Offer系列-面试题39-2:判断一棵树是否为平衡二叉树
- Chapter 4 | Trees and Graphs--检查一棵树是否平衡以及判断一棵树是否为平衡二叉树
- 判断一棵树是否为平衡二叉树
- 判断一棵树是否为平衡二叉树的算法
- 【数据结构】中判断一棵树是否为平衡二叉树
- 剑指off-判断一棵树是否是平衡二叉树
- 判断一棵树是否是平衡二叉树
- Balanced Binary Tree:判断一棵树是否为平衡二叉树
- 判断一棵树是否是平衡二叉树及其时间复杂度的优化
- leetcode 110-判断一棵树是否为平衡二叉树
- AVL树详解&面试题-判断一棵树是否是平衡二叉树
- 面试题——判断一棵树是否是平衡二叉树
- 判断一棵树是否是平衡二叉树
- 测试:判断一棵树是否是平衡二叉树
- 判断一棵树是否为平衡二叉树
- 判断一棵树是否是平衡二叉树
- 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- 《剑指offer》:[39-1]判断是否为平衡二叉树
- 输入一棵二叉树,判断该二叉树是否是平衡二叉树。