4.4

来源:互联网 发布:魔客吧还原数据密码 编辑:程序博客网 时间:2024/06/06 05:38

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.3BFS.print(createLevelLinkedList(root));}}
//结果Link list at depth 0: 5Link list at depth 1: 2 8Link list at depth 2: 1 3 6 9Link list at depth 3: 4 7 90



原创粉丝点击