4.4

Topic:Given a binary search tree, design an algortithm which creates a linked list of all the nodes at each depth (e.g. if you have a tree with depth D, you’ll have D linked lists).

// 关键:前序遍历,根左右;中序遍历,左根右;后序遍历,左右根。

// 方法1BFS。每一层是LinkedList,所有的层放在ArrayList里。循环里把上一层的LinkedList存起来,新建下一层,依次把上一层的左右结点存入这一层。Iterate the root first, then level 2, then level 3 and so on. With each level i, get all nodes on level i-1, simply lokk at their children. Time:O(N). Space: O(N).

// 方法2DFS。递归。每一层都先填一部分,搜索完一个结点,再回来填它。Pass in level+1 to the next recursive call. Time:O(N), O(logN) recursive calls, each  of which adds a new level to the stack. But both require returning O(N) data, so space is O(N). (May actually use more data than BFS).

package w;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;

public class BFS {
	public static ArrayList<LinkedList<TreeNode>> createLevelLinkedList(TreeNode root) {
		ArrayList<LinkedList<TreeNode>> result = new ArrayList<LinkedList<TreeNode>>();

		LinkedList<TreeNode> current = new LinkedList<TreeNode>();
		if (root != null) {
			current.add(root);
		}

		while (current.size() > 0) {
			result.add(current); 
			LinkedList<TreeNode> parents = current; // // 这两句把上一层的LinkedList存起来,开始下一层
			current = new LinkedList<TreeNode>(); 
			for (TreeNode parent : parents) {
				/* Visit the children */
				if (parent.left != null) {
					current.add(parent.left);
				}
				if (parent.right != null) {
					current.add(parent.right);
				}
			}
		}

		return result;
	}

	public static void print(ArrayList<LinkedList<TreeNode>> result){
		int depth = 0;
		for(LinkedList<TreeNode> entry : result) {
			Iterator<TreeNode> i = entry.listIterator();
			System.out.print("Link list at depth " + depth + ":");
			while(i.hasNext()){
				System.out.print(" " + ((TreeNode)i.next()).data);
			}
			System.out.println();
			depth++;
		}
	}

	public static TreeNode createMinimalBST(int arr[], int start, int end){//因为从上往下递归构造BST需要不断把数组折半,所以需要头尾参数,所以该方法分两步来写
    	if (end < start) {
			return null;
		}
    	int mid = (start + end) / 2;
		TreeNode n = new TreeNode(arr[mid]);
		n.setLeftChild(createMinimalBST(arr, start, mid - 1));
		n.setRightChild(createMinimalBST(arr, mid + 1, end));
		return n;
    }
    
    public static TreeNode createMinimalBST(int array[]){
    	return createMinimalBST(array, 0, array.length - 1);
    }

	public static void main(String[] args) {
		int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
		TreeNode root = createMinimalBST(array);
		print(createLevelLinkedList(root));
	}

	}
package w;
import java.util.ArrayList;
import java.util.LinkedList;
public class DFS {
	 public static void createLevelLinkedList(TreeNode root, ArrayList<LinkedList<TreeNode>> arrayList, int level) {
			if (root == null) return;//到了最后一层的下面,没有元素了,就为null,结束
			LinkedList<TreeNode> list = null;//每一次循环都把list清空
			
			if(arrayList.size()==level){//重点在这里,这里用来区分还在一层,还是进入其层了
				list = new LinkedList<TreeNode>();
				arrayList.add(list);  //表示是新的一层
			}
			else{
				list=arrayList.get(level); //表示还在原层
			}
			list.add(root);//注意是先把LinkedList放进ArrayList,再往LinkedList里添加每一层的元素
			createLevelLinkedList(root.left, arrayList, level+1);
			createLevelLinkedList(root.right,arrayList, level+1);
			}
	 
	 public static ArrayList<LinkedList<TreeNode>> createLevelLinkedList(TreeNode root) {
			ArrayList<LinkedList<TreeNode>> lists = new ArrayList<LinkedList<TreeNode>>();
			createLevelLinkedList(root, lists, 0);
			return lists;
		}	
	 
		    public static void main(String[] args) {
				int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
				TreeNode root = BFS.createMinimalBST(array);//调用4.3
				BFS.print(createLevelLinkedList(root));
			}
}
//结果
Link list at depth 0: 5
Link list at depth 1: 2 8
Link list at depth 2: 1 3 6 9
Link list at depth 3: 4 7 90



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值