/*
* 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。
* 注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
*/
public class GetNext {
public TreeLinkNode getNext(TreeLinkNode pNode) {
if(pNode == null) return pNode;
//存在右子树:判断是否存在父节点
if(pNode.right != null) {
TreeLinkNode pRight = pNode.right;
while(pRight.left != null) {
pRight = pRight.left;
}
return pRight;
}
//不存在右子树
while(pNode.next != null) {
if(pNode.next.left == pNode) return pNode.next;
pNode = pNode.next;
}
return null;
}
}
《剑指offer》-找出中序遍历顺序的下一个结点并且返回
最新推荐文章于 2020-08-30 11:37:34 发布