思路:因为是二叉搜索树,所以每个子树的根结点大于左子树,小于右子树。因此二叉树的下一个结点分两种情况讨论,有右结点如6和没有右结点。没有右结点又分为当前结点是左孩子如5还是右孩子如7。
public TreeLinkNode GetNext(TreeLinkNode pNode) {
if(pNode==null) return null;
if(pNode.right!=null){
pNode=pNode.right;
while(pNode.left!=null)
{
pNode=pNode.left;
}
return pNode;
}
while(pNode.next!=null){
if(pNode.next.left==pNode){
return pNode.next;
}
else {
pNode=pNode.next;
}
}
return null;
}