Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than
1 参数传递法:
bool isBalanced(TreeNode *root) {
if(!root) return true;
bool balance = true;
balanceHelper(root, balance);
return balance;
}
void treeHeight(TreeNode *node, int &h, int oneh = 0)
{
if(!node) return;
oneh++;
treeHeight(node->left, h, oneh);
treeHeight(node->right, h, oneh);
if(!node->left && !node->right)
h = oneh > h? oneh:h;
}
void balanceHelper(TreeNode *node, bool &balance)
{
if(!balance || !node) return;
int lh = 0;
int rh = 0;
treeHeight(node->left, lh);
treeHeight(node->right, rh);
if (abs(lh-rh) > 1)
{
balance = false;
return;
}
balanceHelper(node->left, balance);
balanceHelper(node->right, balance);
}
2 返回值:
bool isBalanced2(TreeNode *root) {
if (root == nullptr) return true;
int left = getHeight(root->left);
int right = getHeight(root->right);
if (abs(left - right) > 1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int getHeight(TreeNode *n) {
if (n == nullptr) return 0;
return max(getHeight(n->left), getHeight(n->right)) + 1;
}
3 加判断:
int cntHeight(TreeNode *root) {
if(root == NULL) return 0;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < 0 || r < 0 || abs(l-r) > 1) return -1;
else return max(l, r) + 1;
}
bool isBalanced3(TreeNode *root) {
if(root == NULL) return true;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < 0 || r < 0 || abs(l-r) > 1) return false;
else return true;
}
4 其实可以更加简单:
bool isBalanced5(TreeNode *root) {
return (height_(root) >= 0 );
//or: return cntHeight(root) >=0;
}
int height_(TreeNode *root){
if (root == NULL) return 0;
int lh = height_(root->left);
if (lh == -1 ) return lh;
int rh = height_(root->right);
if (rh == -1 ) return rh;
int d = rh - lh;
if (d > 1 || d < -1) return -1;
else return 1 + ( d > 0 ? rh : lh );
}
5 或者:
bool isBalanced(TreeNode *root) {
return height(root) != -1;
}
int height(TreeNode *root)
{
if(root == nullptr)
return 0;
int leftHeight = height(root->left);
if(leftHeight == -1)
return -1;
int rightHeight = height(root->right);
if(rightHeight == -1)
return -1;
if(abs(leftHeight - rightHeight) > 1)
return -1;
return 1 + max(leftHeight, rightHeight);
}
6 还有:
bool f(TreeNode *root, int &d) {
if (!root) {
d = 0;
return true;
}
int ld, rd;
bool lb = f(root->left, ld);
bool rb = f(root->right, rd);
d = ld > rd ? ld+1:rd+1;
if (abs(ld-rd)>1) return false;
return lb && rb;
}
bool isBalanced8(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int depth;
return f(root, depth);
}
部分程序出处:
LeetCode
2014-1-17 update
bool isBalanced(TreeNode *root)
{
if (!root) return true;
if (!isBalanced(root->left)) return false;
if (!isBalanced(root->right)) return false;
if (abs(treeDepth(root->left)-treeDepth(root->right)) > 1)
return false;
return true;
}
int treeDepth(TreeNode *r)
{
if (!r) return 0;
return max(treeDepth(r->left), treeDepth(r->right)) +1;
}
分享到:
相关推荐
平衡二叉树,又称为AVL树,是1962年由Adelsonr Velskii 和Landis 提出并以他们的名字命名的,它是具有如下性质的二叉树:
leetcode的题目:Balanced Binary Tree
手动输入数据,加入到二叉树中,并生成平衡二叉树,对学习C++及数据结构有较大帮助
平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),它具有以下性质: 1. 它是一棵空树或它的左右两个子树的高度差的绝对值不超过1。 2. 左右两个子树都是一棵平衡二叉树。 平衡二叉树大部分操作和...
平衡二叉树(Balanced Binary Tree),简称平衡树(AVL树):树上任一结点的左子树和右子树的高度之差不超过1。平衡二叉树的一系列操作:删除、插入(在二叉排序树中插入新结点后,如何保持平衡)、调整平衡等等等。...
形态匀称的二叉树称为平衡二叉树 (Balanced binary tree) ,其严格定义是:一棵空树是平衡二叉树;若 T 是一棵非空二叉树,其左、右子树为 TL 和 TR ,令 hl 和 hr 分别为左、右子树的深度。
①平衡二叉树(Balanced Binary Tree)是指树中任一结点的左右子树的高度大致相同。 ②任一结点的左右子树的高度均相同(如满二叉树),则二叉树是完全平衡的。通常,只要二叉树的高度为O(1gn),就可看作是平衡的。 ...
平衡二叉树(Balanced Binary Tree)又被称为AVL树。具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。构造与调整方法。 实验目的: 二叉排序树的实现...
平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树(递归定义)的二叉排序树。...
This is a survey paper on balanced binary mappings defined on graphs. It contains proofs of several basic results.
“Size Balanced Tree (abbr. SBT) 是一种通过大小来保持平衡的BST。” SBT资料——陈启峰本人 ppt中文版 若需参考更多资料,请下载“陈启峰《Size Balanced Tree》 pdf英文版”
基于平衡二叉树的C语言MAP,可以根据用户传入的类型自定义KEY和VAL的类型,如int or 结构体
基本实现平衡二叉树的创建,插入和删除操作。使用C++代码实现
1.5 二叉树是否平衡Given a binary tree, determine if it is height-balanced.For this prob
Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from...
我自己写的SBTree的实现 参考了http://www.nocow.cn/index.php/Size_Balanced_Tree的算法 声明:此源码可以用于商业用途,但请注明来源于random
“Size Balanced Tree (abbr. SBT) 是一种通过大小来保持平衡的BST。” SBT资料——陈启峰本人 pdf英文版 若需参考更多资料,请下载“陈启峰《Size Balanced Tree》 ppt中文版”
数据结构,平衡二叉树平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。...
平衡二叉树/AVL 树(Balanced Binary Tree/AVL Tree) 红黑树(Red-Black Tree) 伸展树(Splay Tree) B-树(B-Tree) 线索二叉树(Threaded Binary Tree) 前缀树/字典树(Trie) 5. 哈希/散列(Hashing) 哈希表...
平衡二叉树/AVL 树(Balanced Binary Tree/AVL Tree) 红黑树(Red-Black Tree) 伸展树(Splay Tree) B-树(B-Tree) 线索二叉树(Threaded Binary Tree) 前缀树/字典树(Trie) 5. 哈希/散列(Hashing) 哈希表...