Cracking the coding interview--Q4.4

原文:

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();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值