题目
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
分析
根据中序遍历的特点,要找到一个节点的下一个节点无非就是三种情况:
1、有右子树,这时只需要把其右孩子作为下一个遍历的(并不是要找的)节点,然后沿着该节点的左子树(如果有的话)出发,直到遇到叶子节点,那么该叶子节点就是其下一个要找的节点;
2、没有右子树,则判断该节点是否是其父节点的左孩子,如果是则其下一个要找的节点是其父节点;
3、如果不是其父节点的左孩子,则把其父节点作为下一个遍历的节点,向上回溯,直到找到节点没有父节点或者节点是其父节点的左孩子为止。
综合这三种情况就可以找到二叉树中任意一个节点的下一个节点。
代码
/**
* 二叉树节点
*
* @author: xuwenjie01
**/
public class BinaryTreeNode {
private int value;
private BinaryTreeNode left;
private BinaryTreeNode right;
private BinaryTreeNode father;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
public BinaryTreeNode getFather() {
return father;
}
public void setFather(BinaryTreeNode father) {
this.father = father;
}
}
/**
* @author: xuwenjie01
**/
public class FindNextNode {
public BinaryTreeNode getNextNode(BinaryTreeNode pNode) {
// 入参检查
if (pNode == null) {
return null;
}
// 临时节点
BinaryTreeNode tempNode = null;
// 如果该节点有右子节点
if (pNode.getRight() != null) {
tempNode = pNode.getRight();
while (tempNode.getLeft() != null) {
tempNode = tempNode.getLeft();
}
return tempNode;
}
// 如果该节点没有右子节点,它是其父节点的左子节点
if (pNode.getFather() == null) {
return null;
}
if (pNode.getFather().getLeft() == pNode) {
return pNode.getFather();
}
// 如果该节点没有右子节点,它是其父节点的右子节点
tempNode = pNode.getFather();
while (tempNode.getFather() != null) {
if (tempNode.getFather().getLeft() == tempNode) {
return tempNode.getFather();
}
//继续向上找父节点
tempNode = tempNode.getFather();
}
return null;
}
}