在二叉树中找到一个节点的后继节点
作者:互联网
在二叉树的中序遍历的序列中,node的下一个节点叫作node的后继节点。
该结构比普通二又树节点结构多了一个指向父节点的parent指针。
假设有一棵Node类型的节点组成的二叉树,树中每个节点的parent指针都正确地指向自己的父节点,头节点的parent指向null。
只给一个在二叉树中的某个节点node,请实现返回node的后继节点的函数。
思路:找节点x的后继节点
代码:
package Algorithms.tree; public class 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 node) { if (node == null) { return node; } if (node.right != null) { //有右子树 return getLeftMost(node.right); } else { //无右子树 Node parent = node.parent; //找到其父节点 //while循环找当前节点是其父节点的左子树 while (parent != null && parent.left != node) { //当前节点是其父节点的右子树 node = parent; //node往上走到parent的位置 parent = node.parent; //parent来到node父节点的位置 } //当前节点是其父节点的左子树 或者 父节点为空(node是整棵树最右节点时)时退出while循环 return parent; //退出循环说明找到了后继节点 } } //返回一个节点子树中最左的Node public static Node getLeftMost(Node node) { if (node == null) { return node; } while (node.left != null) { node = node.left; } return node; } public static void main(String[] args) { Node head = new Node(6); 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,2,3,4,5,6,7,8,9 /** * 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 */
标签:Node,head,right,parent,后继,二叉树,test,节点,left 来源: https://www.cnblogs.com/zh-xiaoyuan/p/15134501.html