由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 算法题:如何判断二叉树是AVL?
相关主题
判断BT是否BST?问一个题目
大虾帮忙看看代码,为什么 res参数传入不了,返回总是nullTime complexity
how to check a bin tree is balanced?写了个symmetric tree的stack based iterative实现,有个bug
菜鸟问一道java题目,check balanced binary tree判断是不是binary search tree-leetcode
Twitter电面未通过hasPathSum
回馈本版,新鲜店面,新题新气象G家电面
热腾腾的 LinkedIn 电面题攒RPleetcode valid bst new test cases 过不去了。。。
一道google面试题fb面经的一题
相关话题的讨论汇总
话题: root话题: getheight话题: height话题: left话题: return
进入JobHunting版参与讨论
1 (共1页)
q****x
发帖数: 7404
1
这个递归解法简洁但低效。应该用DP,在hash table里把每个节点和子树深度记下来?
int getHeight(Node* root)
{
if (root === NULL) return 0;
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
bool isAVL(Node* root)
{
if (root == NULL) return true;
return abs(getHeight(root->left) - getHeight(root->right)) <= 1
&& isAVL(root->left) && is AVL(root->right);
}
s******n
发帖数: 3946
2
bool isAVLTree(Node* n, int* height)
{
if (n==NULL) {
*height = 0;
return true;
}
int left_height, right_height;
if (isAVLTree(n->left, &left_height) && isAVLTree(n->right, &right_height) && abs(left_height-right_height)<=1) {
*height = max(left_height, right_height)+1;
return true;
} else {
return false;
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
fb面经的一题Twitter电面未通过
Interview question::回馈本版,新鲜店面,新题新气象
BST面试题热腾腾的 LinkedIn 电面题攒RP
二叉树按层次打印有没有办法换行显示?一道google面试题
判断BT是否BST?问一个题目
大虾帮忙看看代码,为什么 res参数传入不了,返回总是nullTime complexity
how to check a bin tree is balanced?写了个symmetric tree的stack based iterative实现,有个bug
菜鸟问一道java题目,check balanced binary tree判断是不是binary search tree-leetcode
相关话题的讨论汇总
话题: root话题: getheight话题: height话题: left话题: return