i**9 发帖数: 351 | 1 常见的解法:
//assume v1
Node * commanancestor(Node * root,int v1,int v2){
if(root == NULL){
return NULL;
}
Node * current = root;
while(current != NULL){
if(current->data < v2 && current->data > v1){
return current;
}
if(current->data > v2){
current = current->left;
}
if(current->data < v1){
current = current->right;
}
}
return current;
}
我怎么觉得这个不全面(有的请款没有涵盖),找都一个比较全面的
http://geeksforgeeks.org/?p=1029
大家讨论讨论 | s*****n 发帖数: 5488 | 2 要写一个 industrial 强度的代码,这个还差一些。
第一
assert (v1 <= v2);
special cases:
data == v1,
data == v2;
&& v1 =v2; this reduces to bst search and what if there is no data == v1 (v2
)?
both algorithms will die in the final special case.
one : infinite loop
another: return with nothing ( ususally, cc will help you with a warning as
error).
【在 i**9 的大作中提到】 : 常见的解法: : //assume v1: Node * commanancestor(Node * root,int v1,int v2){ : if(root == NULL){ : return NULL; : } : Node * current = root; : while(current != NULL){ : if(current->data < v2 && current->data > v1){ : return current;
| s*****n 发帖数: 5488 | 3 btw, programing interview exposed上面的答案基本上都非常buggy的。
v2
【在 s*****n 的大作中提到】 : 要写一个 industrial 强度的代码,这个还差一些。 : 第一 : assert (v1 <= v2); : special cases: : data == v1, : data == v2; : && v1 =v2; this reduces to bst search and what if there is no data == v1 (v2 : )? : both algorithms will die in the final special case. : one : infinite loop
| i**9 发帖数: 351 | 4 thanks, 我觉得第二个解法还可以吧?
【在 s*****n 的大作中提到】 : btw, programing interview exposed上面的答案基本上都非常buggy的。 : : v2
| s*****n 发帖数: 5488 | 5 没有什么基本可以。
你越professional,越容易拿到offer.
编程序就是很琐碎的事情。
【在 i**9 的大作中提到】 : thanks, 我觉得第二个解法还可以吧?
|
|