由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 讨论 Lowest common ancestor of BST
相关主题
Lowest common ancestor of two nodes of Binary TreeF家电面
一个老题binary tree找 lowest common ancestor 的code (请教least common ancestor的疑惑
Lowest Common Ancestor in a binary tree (no parent pointer)G家intern电面新鲜面经
求教Leetcode题目:Lowest Common AncestorBST面试题
Lowest Common Ancestor of multiple nodes in a binary tree刚才的amazon phone interview 第一轮
请问如何求binary tree的lowest common ancestor求教:binary search tree中找第i大的数
二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?amazon on-site interview
请问二叉搜索树如何找到两个点的最近祖先?哪个高手能指出我程序的问题 (20几行的代码)
相关话题的讨论汇总
话题: current话题: v2话题: v1话题: null话题: node
进入JobHunting版参与讨论
1 (共1页)
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, 我觉得第二个解法还可以吧?
1 (共1页)
进入JobHunting版参与讨论
相关主题
哪个高手能指出我程序的问题 (20几行的代码)Lowest Common Ancestor of multiple nodes in a binary tree
这个check whether a binary tree is a BST 问题请问如何求binary tree的lowest common ancestor
这个check whether a binary tree is a BST or not二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?
发几道Google面试题(Phone and Onsite)请问二叉搜索树如何找到两个点的最近祖先?
Lowest common ancestor of two nodes of Binary TreeF家电面
一个老题binary tree找 lowest common ancestor 的code (请教least common ancestor的疑惑
Lowest Common Ancestor in a binary tree (no parent pointer)G家intern电面新鲜面经
求教Leetcode题目:Lowest Common AncestorBST面试题
相关话题的讨论汇总
话题: current话题: v2话题: v1话题: null话题: node