【题目】 现在有一种新的二叉树节点类型如下:

public class Node { 

public int value;

public Node left;

public Node right;

public Node parent;

public Node(int data) {

this.value = data;

}

}


    该结构比普通二叉树节点结构多了一个指向父节点的parent指针。假设有一 棵Node类型的节点组成的二叉树,
    树中每个节点的parent指针都正确地指向 自己的父节点,头节点的parent指向null。
    只给一个在二叉树中的某个节点 node,请实现返回node的后继节点的函数。在二

    叉树的中序遍历的序列中, node的下一个节点叫作node的后继节点。

题目的意思是定义上述的新型二叉树,然后给定任意一个节点,让我们找出给定节点的后继节点。什么是后继节点呢?后继节点就是一颗二叉树的中序遍历得到的假如是:1,2,3.....那么1的后继节点就是2,2的后继节点就是3.....

思路:新型二叉树,多了一个父节点,根节点的父节点是空
1.当一个节点有右子树的时候,他的后继节点是她的右子树的最左子节点

2.当一个节点没右子树的时候,她的后继节点是他的第一个子节点是父节点的左子节点的时候 (她的父节点)

举个栗子,看图:

如何在二叉树中找到一个节点的后继节点_后继

这棵树的中序遍历是:1,2,3,4,5,6,7,8,9,10

假如给定的是4节点,由于4节点右子节点存在,而4的右子节点的最左子节点就是他自己,所以4的后继节点就是5..加入给定的是2节点,2没有右子节点,就是情况2了,设2节点是当前节点cur,cur的父节点是parent,这个时候,parent节点的左子节点并不是cur,所以cur和parent节点都往上移动一个节点,这个时候parent节点3的左子节点正好子cur节点1,所以2节点的后继节点就是parent节点3了。

下面看代码:

public class C03_SuccessorNode {
public static class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int data) {
this.value = data;
}
}
public static Node getSuccessorNode(Node cur){
if(cur == null){
return cur;
}
if(cur.right != null){//当一个节点有右子树的时候
Node node = cur.right;
while(node.left != null){
node = node.left;
}
return node;
}else {//当一个节点没有右子节点的时候
Node parent = cur.parent;
while(parent != null && parent.left != cur){
cur = cur.parent;
parent = cur.parent;
}
return parent;
}
}
public static void main(String[] args) {
Node head = new Node(6);
//建立一棵树。中序遍历是1、2、3、4、5、6、7、8、9、10(形成的二叉树就是上面的图片的)
head.parent = null;
head.left = new Node(3);
head.left.parent = head;
head.left.left = new Node(1);
head.left.left.parent = head.left;
head.left.left.right = new Node(2);
head.left.left.right.parent = head.left.left;
head.left.right = new Node(4);
head.left.right.parent = head.left;
head.left.right.right = new Node(5);
head.left.right.right.parent = head.left.right;
head.right = new Node(9);
head.right.parent = head;
head.right.left = new Node(8);
head.right.left.parent = head.right;
head.right.left.left = new Node(7);
head.right.left.left.parent = head.right.left;
head.right.right = new Node(10);
head.right.right.parent = head.right;

//测试
Node test = head.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right; // 10's next is null
System.out.println(test.value + " next: " + getSuccessorNode(test));
/*
测试结果:
1 next: 2
2 next: 3
3 next: 4
4 next: 5
5 next: 6
6 next: 7
7 next: 8
8 next: 9
9 next: 10
10 next: null
*/
}
}