原文:
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).
译文:
给定一棵二叉查找树,设计算法,将每一层的所有结点构建为一个链表(也就是说, 如果树有D层,那么你将构建出D个链表)
使用链表数组记录每层的信息,具体每层使用链表记录该层的所有结点信息
使用先序遍历二叉树,同时遍历的时候记录该层为第几层,并把该层结点加入该层链表信息
public static void getLinkList(Node_4_3 node, int censhu) {
censhu ++;
if(node != null) {
LinkNode linkNode = new LinkNode();
linkNode.data = node.data;
linkNode.next = null;
if(linkList[censhu-1].first == null) {
linkList[censhu-1].first = linkNode;
} else {
LinkNode p = linkList[censhu-1].first;
while(p.next != null) {
p = p.next;
}
p.next = linkNode;
}
getLinkList(node.lchild, censhu);
getLinkList(node.rchild, censhu);
}
}
最后遍历该链表信息,输出
public static void printLinkList() {
for(int i=0; i<linkList.length; i++) {
System.out.format("第 %d 层 : ", (i+1));
LinkNode linkNode = linkList[i].first;
while(linkNode != null) {
System.out.format(" %d ", linkNode.data);
linkNode = linkNode.next;
}
System.out.format("\n");
}
}
总的代码如下:
package chapter_4_TreesandGraphs;
import java.util.Scanner;
class LinkNode {
public int data;
public LinkNode next;
}
class LinkList {
public int censhu;
public LinkNode first;
}
/**
*
* 给定一棵二叉查找树,设计算法,将每一层的所有结点构建为一个链表(也就是说, 如果树有D层,那么你将构建出D个链表)
*
*/
public class Question_4_4 {
private static LinkList[] linkList;
public static void create_MinHeight_Tree(Node_4_3 parent, int array[], int start, int end, boolean left) {
if(start <= end) {
int mid = (start + end) >> 1;
Node_4_3 node = new Node_4_3();
node.data = array[mid];
node.parent = parent;
// 构建左子树
if(left == true) {
parent.lchild = node;
} else {
parent.rchild = node;
}
create_MinHeight_Tree(node, array, start, mid-1, true);
create_MinHeight_Tree(node, array, mid+1, end, false);
}
}
public static int getMinHeight(Node_4_3 node) {
if(node == null) {
return 0;
}
return 1 + Math.max(getMinHeight(node.lchild), getMinHeight(node.rchild));
}
/**
* @param node
* @param censhu
* 构建
*
*/
public static void getLinkList(Node_4_3 node, int censhu) {
censhu ++;
if(node != null) {
LinkNode linkNode = new LinkNode();
linkNode.data = node.data;
linkNode.next = null;
if(linkList[censhu-1].first == null) {
linkList[censhu-1].first = linkNode;
} else {
LinkNode p = linkList[censhu-1].first;
while(p.next != null) {
p = p.next;
}
p.next = linkNode;
}
getLinkList(node.lchild, censhu);
getLinkList(node.rchild, censhu);
}
}
/**
* 输出
*/
public static void printLinkList() {
for(int i=0; i<linkList.length; i++) {
System.out.format("第 %d 层 : ", (i+1));
LinkNode linkNode = linkList[i].first;
while(linkNode != null) {
System.out.format(" %d ", linkNode.data);
linkNode = linkNode.next;
}
System.out.format("\n");
}
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
String strs[] = string.split(" ");
int array[] = new int[strs.length];
for(int i=0; i<array.length; i++) {
array[i] = Integer.parseInt(strs[i]);
}
Node_4_3 head = new Node_4_3();
int mid = (0 + (array.length-1)) >> 1;
head.data = array[mid];
create_MinHeight_Tree(head, array, 0, mid-1, true);
create_MinHeight_Tree(head, array, mid+1, array.length-1, false);
int height = getMinHeight(head);
linkList = new LinkList[height];
for(int i=0; i<height; i++) {
linkList[i] = new LinkList();
linkList[i].censhu = i + 1;
}
getLinkList(head, 0);
printLinkList();
}
}