二叉树的最低公共祖先
中序遍历的后继
- 有右子树 则是右子树上最左节点
- 没有右子树 后继Y 符合左树中最右的节点是x ( 从当前节点 一直往上找 是不是父节点的左子树 是就找到 不是继续往上 找到根了都找不到 那就没有后继 )
static Node gethouji(Node node) {
if (node.right != null) {//有右子树 就找右子树中最左的节点
node = getrightleft(node);
return node;
} else {//没有右子树 就看当前节点是不是父节点的 左孩子节点
//是 就返回结果 不是就继续找上一个儿子节点
Node parent = node.parent;
while (parent != null && parent.right == node) {
node = parent;
parent = node.parent;
}
if (parent == null) {
return null;
}
if (parent.left == node) {
return parent;
}
}
return null;
}
static Node getrightleft(Node node) {
node = node.right;
while (node.left != null) {
node = node.left;
}
return node;
}
static class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int data) {
this.value = data;
}
}
二叉树的序列化和反序列化
(中序 或者后序 还有按层序列化)
package tree;
import java.util.LinkedList;
public class serializetree {
static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
static StringBuffer serialize(Node node) {
if (node == null) {
return new StringBuffer("#_"); //basecase
}
StringBuffer str = new StringBuffer(node.value + "_"); //当前节点去构成串
StringBuffer sb1 = serialize(node.left);
StringBuffer sb2 = serialize(node.right);
return str.append(sb1).append(sb2);//左边加右边 然后按照根左右的顺序拼接
}
static Node reserialize(StringBuffer str) {
String[] strings = str.toString().split("_");
LinkedList queue = new LinkedList();
for (int i = 0; i < strings.length; i++) {
queue.add(strings[i]);
}
Node preserialize = preserialize(queue);//进入递归去
return preserialize;
}
static Node preserialize(LinkedList<String> queue) {
String str = queue.poll();//弹出节点
if (str.equals("#")) {//#号就不用构建节点了
return null;
}
Node node = new Node(Integer.valueOf(str));
node.left = preserialize(queue);//构建左右树去拼接当前的节点
node.right = preserialize(queue);
return node;//返回当前节点
}
public static void main(String[] args) {
Node head1 = new Node(1);
Node head2 = new Node(2);
Node head3 = new Node(3);
Node head4 = new Node(4);
Node head5 = new Node(5);
head1.left = head2;
head1.right = head3;
head2.left = head4;
head2.right = head5;
StringBuffer sb = serialize(head1);
System.out.println(sb);
Node head = reserialize(sb);
StringBuffer sb2 = serialize(head);
System.out.println("反序列化之后------");
System.out.println(sb2);
}
}
按层遍历的序列化和反序列化
public static String serialByLevel(Node head) {
if (head == null) {
return "#!";
}
String res = head.value + "!";
Queue<Node> queue = new LinkedList<Node>();
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
if (head.left != null) {
res += head.left.value + "!";
queue.offer(head.left);
} else {
res += "#!";//为空就加入#!
}
if (head.right != null) {
res += head.right.value + "!";
queue.offer(head.right);
} else {
res += "#!";//为空就加入#!
}
}
return res;
}
public static Node reconByLevelString(String levelStr) {
String[] values = levelStr.split("!");
int index = 0;
Node head = generateNodeByString(values[index++]);
Queue<Node> queue = new LinkedList<Node>();
if (head != null) {
queue.offer(head);
}
Node node = null;
while (!queue.isEmpty()) {
node = queue.poll();
node.left = generateNodeByString(values[index++]);
// 把字符串加进去 搞节点出来并作为当前节点的子节点
node.right = generateNodeByString(values[index++]);
// 把字符串加进去 搞节点出来并作为当前节点的子节点
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return head;
}
public static Node generateNodeByString(String val) {
if (val.equals("#")) {
return null;
}
return new Node(Integer.valueOf(val));
}
网友评论