由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 麻烦谁贴一个bug free的BST next node
相关主题
想到一道老题这最小公共父母节点有bug吗?
问题在哪儿啊 kth Node of BST,大家帮忙大家帮忙看看 问题在哪啊?由preorder 来建 bst,为什么后面没
amazon on-site interview一道在线题
树 inorder下个节点最好办法是啥请教一道g算法题
一道二叉树的老题求教Leetcode题目:Lowest Common Ancestor
问个google面试题BST面试题
请教linked list, 删除最后一个节点求教:binary search tree中找第i大的数
插入节点到complete binary tree的末尾一个老题binary tree找 lowest common ancestor 的code (请教
相关话题的讨论汇总
话题: node话题: root话题: null话题: key话题: return
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
输入是一个node*, 如果是node*那么不能仅仅根据值来搜索这个节点, 因为不同的节点
可以对应同一个值, 最差还是得搜索n次
w****x
发帖数: 2483
2
输入是一个node*, 如果是node*那么不能仅仅根据值来搜索这个节点, 因为不同的节点
可以对应同一个值, 最差还是得搜索n次
a*******8
发帖数: 110
3
有parent pointer吗?
z*********8
发帖数: 2070
4
考虑duplicate值就太没意思了

【在 w****x 的大作中提到】
: 输入是一个node*, 如果是node*那么不能仅仅根据值来搜索这个节点, 因为不同的节点
: 可以对应同一个值, 最差还是得搜索n次

H****r
发帖数: 2801
5
search (*node+1) ?

【在 w****x 的大作中提到】
: 输入是一个node*, 如果是node*那么不能仅仅根据值来搜索这个节点, 因为不同的节点
: 可以对应同一个值, 最差还是得搜索n次

m********g
发帖数: 272
6
public static Node successorInBST(Node root, int key)
{
Node pos = findInBST(root, key);

if(pos == null)
{
throw new IllegalArgumentException("Cannot find the key in the
BST");
}

if(pos.getRight() != null)
{
return findSmallest(pos.getRight());
}

Node successor = null;

while(root != null)
{
if(key < root.getKey())
{
successor = root;
root = root.getLeft();
}
else if(key > root.getKey())
{
root = root.getRight();
}
else
{
break;
}
}

return successor;
}

private static Node findSmallest(Node root)
{
Validate.notNull(root, "root cannot be null");

while(root.getLeft() != null)
{
root = root.getLeft();
}

return root;
}

private static Node findInBST(Node root, int key)
{
Node p = root;

while(p != null)
{
if(p.getKey() == key)
{
return p;
}
else if(p.getKey() > key)
{
p = p.getLeft();
}
else
{
p = p.getRight();
}
}

return null;
}
m********g
发帖数: 272
7
编译过,但是没运行过。
let me know, if you find any bugs.
1 (共1页)
进入JobHunting版参与讨论
相关主题
一个老题binary tree找 lowest common ancestor 的code (请教一道二叉树的老题
判断BT是否BST?问个google面试题
find the k-th maximum node in a binary search tree 有疑问请教linked list, 删除最后一个节点
发现一个很恶心的基础问题插入节点到complete binary tree的末尾
想到一道老题这最小公共父母节点有bug吗?
问题在哪儿啊 kth Node of BST,大家帮忙大家帮忙看看 问题在哪啊?由preorder 来建 bst,为什么后面没
amazon on-site interview一道在线题
树 inorder下个节点最好办法是啥请教一道g算法题
相关话题的讨论汇总
话题: node话题: root话题: null话题: key话题: return