由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问我写的这个判断tree是否balance的code有问题么?
相关主题
请教一道面试题Amazon phone interview Software Engineering Intern
Programming interview exposed 上面的那道NULL or Cycle的linked list题问一个C++的binary search tree类实现问题 (转载)
Find LeastCommonAncestor for N-ary Tree面试的时候 binary tree的delete也要15分钟之内写完么?
Google Intern 面试 【请教】求教一道面试题
Lowest common ancestor of two nodes of Binary Tree弱问怎么判断两个binary tree相同?
delete a node in linked listLeetcode bst max path-----is this solution correct?
热腾腾的 LinkedIn 电面题攒RPHaker Rank Median...
判断 bst 疑问问一个问题: binary search Tree的删除用java实现。
相关话题的讨论汇总
话题: head话题: null话题: left话题: return话题: right
进入JobHunting版参与讨论
1 (共1页)
c***g
发帖数: 472
1
我个人对自己的corner case的考虑一向没有信心,请各位大侠调教一下
int
Tree::getDepth(Node* head) {

if(head == NULL) return 0;
if(head->left == NULL && head->right == NULL) return 0;
int left = getDepth(head->left)+1;
int right = getDepth(head->right)+1;
return (left > right)?left:right;
}
bool
Tree::isBalanced(Node* head) {
if(head == NULL ) return true;

return (isBalanced(head->left)
&& isBalanced(head->right)
&& abs(getDepth(head->left) - getDepth(head->right))<=1) ;


}
l*****a
发帖数: 14598
2
你把corner case作为你程序的测试项不好吗?

【在 c***g 的大作中提到】
: 我个人对自己的corner case的考虑一向没有信心,请各位大侠调教一下
: int
: Tree::getDepth(Node* head) {
:
: if(head == NULL) return 0;
: if(head->left == NULL && head->right == NULL) return 0;
: int left = getDepth(head->left)+1;
: int right = getDepth(head->right)+1;
: return (left > right)?left:right;
: }

c***g
发帖数: 472
3
关键我想象不到有什么其他的corner case啊

【在 l*****a 的大作中提到】
: 你把corner case作为你程序的测试项不好吗?
l*****a
发帖数: 14598
4
我错了
没有细看你的文字
hoho

【在 c***g 的大作中提到】
: 关键我想象不到有什么其他的corner case啊
B*******1
发帖数: 2454
5
弱问
if(head->left == NULL && head->right == NULL) return 0;
不是return 1?
B*******1
发帖数: 2454
6
if(head == NULL) return 0;
if(head->left == NULL && head->right == NULL) return 0;
这code是不是有点啰嗦啊,
只是if(head == NULL) return 0; 就够了吧。
c***g
发帖数: 472
7
如果一个根节点,一个叶子节点,depth是1 吧?

【在 B*******1 的大作中提到】
: if(head == NULL) return 0;
: if(head->left == NULL && head->right == NULL) return 0;
: 这code是不是有点啰嗦啊,
: 只是if(head == NULL) return 0; 就够了吧。

r*******n
发帖数: 266
8
return (maxDep(r) - minDep(r)) > 1
def maxDep(r):
if r == None: return 0
else:
return 1 + max(maxDep(r.left),maxDep(r.right))
def minDep(r):...

【在 c***g 的大作中提到】
: 我个人对自己的corner case的考虑一向没有信心,请各位大侠调教一下
: int
: Tree::getDepth(Node* head) {
:
: if(head == NULL) return 0;
: if(head->left == NULL && head->right == NULL) return 0;
: int left = getDepth(head->left)+1;
: int right = getDepth(head->right)+1;
: return (left > right)?left:right;
: }

B*******1
发帖数: 2454
9
http://www.geeksforgeeks.org/archives/5230
int height(struct node* node)
{
/* base case tree is empty */
if(node == NULL)
return 0;
/* If tree is not empty then height = 1 + max of left
height and right heights */
return 1 + max(height(node->left), height(node->right));
}

【在 c***g 的大作中提到】
: 如果一个根节点,一个叶子节点,depth是1 吧?
d********w
发帖数: 363
10
这里面有不少重复计算吧,我面试时也遇到,面试官希望扫描一遍tree就判断出来
int isB(Tree t){
if(!t) return 0;
int left=isB(t.left);
int right=isB(t.right);
if( left >=0 && right >=0 && left - right <= 1 || left -right >=-1)

return (left else return -1;
}

【在 c***g 的大作中提到】
: 我个人对自己的corner case的考虑一向没有信心,请各位大侠调教一下
: int
: Tree::getDepth(Node* head) {
:
: if(head == NULL) return 0;
: if(head->left == NULL && head->right == NULL) return 0;
: int left = getDepth(head->left)+1;
: int right = getDepth(head->right)+1;
: return (left > right)?left:right;
: }

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个问题: binary search Tree的删除用java实现。Lowest common ancestor of two nodes of Binary Tree
Find the node with given value in binary tree in in-orderdelete a node in linked list
Find a sub tree with min weight怎么做热腾腾的 LinkedIn 电面题攒RP
问个题,用递归方法判断 bst 疑问
请教一道面试题Amazon phone interview Software Engineering Intern
Programming interview exposed 上面的那道NULL or Cycle的linked list题问一个C++的binary search tree类实现问题 (转载)
Find LeastCommonAncestor for N-ary Tree面试的时候 binary tree的delete也要15分钟之内写完么?
Google Intern 面试 【请教】求教一道面试题
相关话题的讨论汇总
话题: head话题: null话题: left话题: return话题: right