题目
调整搜索二叉树中两个错误的节点
java代码
package com.lizhouwei.chapter3;
import java.util.Stack;
/**
* @Description:调整搜索二叉树中两个错误的节点
* @Author: lizhouwei
* @CreateDate: 2018/4/17 19:43
* @Modify by:
* @ModifyDate:
*/
public class Chapter3_10 {
public Node[] getTwoErrNode(Node head) {
Node pre = null;
Node[] errNode = new Node[2];
Stack stack = new Stack<>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
if (pre != null && pre.value > head.value) {
errNode[0] = errNode[0] == null ? pre : errNode[0];
errNode[1] = head;
}
pre = head;
head = head.right;
}
}
return errNode;
}
public Node[] getTwoErrParents(Node head, Node head1, Node head2) {
Node pre = null;
Node[] errParents = new Node[2];
Stack stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
head = stack.pop();
if (head.left == head1 || head.right == head1) {
errParents[0] = head;
}
if (head.left == head2 || head.right == head2) {
errParents[1] = head;
}
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
return errParents;
}
//分14中情况
public Node adjustTree(Node head) {
Node[] errNode = getTwoErrNode(head);
Node e1 = errNode[0];
Node e1L = e1.left;
Node e1R = e1.right;
Node e2 = errNode[1];
Node e2L = e2.left;
Node e2R = e2.right;
Node[] parents = getTwoErrParents(head, e1, e2);
Node e1P = parents[0];
Node e2P = parents[1];
e1.left = e2L;
e1.right = e2R;
e2.left = e1L;
e2.right = e1R;
if (e1 == head) {
if (e1.right == e2) {
e2.right = e1;
} else if (e2P.left == e2) {
e2P.left = e1;
} else if (e2P.right == e2) {
e2P.right = e1;
}
return e2;
} else if (e2 == head) {
if (e2.left == e1) {
e1.left = e2;
} else if (e1P.left == e1) {
e1P.left = e2;
} else if (e1P.right == e1) {
e1P.right = e2;
}
return e1;
} else {
if (e1.left == e2) {
e2.left = e1;
if (e1P.left == e1) {
e1P.left = e2;
} else {
e1P.right = e2;
}
} else if (e2.right == e1) {
e1.right = e2;
if (e2P.left == e2) {
e2P.left = e1;
} else {
e2P.right = e1;
}
} else {
if (e1P.left == e1) {
e1P.left = e2;
if (e2P.left == e2) {
e2P.left = e1;
} else {
e2P.right = e1;
}
} else {
e1P.right = e2;
if (e2P.left == e2) {
e2P.left = e1;
} else {
e2P.right = e1;
}
}
}
}
return head;
}
//测试
public static void main(String[] args) {
Chapter3_10 chapter = new Chapter3_10();
Node head = new Node(3);
head.left = new Node(5);
head.right = new Node(4);
head.left.left = new Node(1);
head.right.right = new Node(2);
Node[] errNode = chapter.getTwoErrNode(head);
Node errNode1 = errNode[0];
Node errNode2 = errNode[1];
System.out.println("中序数组{1, 5, 3, 4, 2}中错误节点:" + errNode1.value + " 和 " + errNode2.value);
Node[] errParents = chapter.getTwoErrParents(head, errNode1, errNode2);
Node err1P = errParents[0];
Node err2P = errParents[1];
System.out.println("错误节点 " + errNode1.value + " 的父节点:" + (err1P == null ? "" : err1P.value));
System.out.println("错误节点 " + errNode2.value + " 的父节点:" + (err2P == null ? "" : err2P.value));
System.out.println("调整错误节点错误节点:" + errNode1.value + " 和 " + errNode2.value);
head = chapter.adjustTree(head);
System.out.print("调整后节点的中序遍历:");
NodeUtil.inOrder(head);
}
}
结果
做个稳妥的人,不要高估两年内的自己,也不要低估十年后的自己。