FWNX-creates a linked list of all the nodes at each depth-JAVA VERSION
2013-12-29 08:11
435 查看
static ArrayList<LinkedList<Node>> mygetLevelNode(Node root) { //initial the linked list ArrayList<LinkedList<Node>> result = new ArrayList<LinkedList<Node>>(); LinkedList<Node> ll = new LinkedList<Node>(); ll.add(root); int level = 0; result.add(level,ll); while(true) { //for every row there is a linked list we firstly create it LinkedList<Node> ll2 = new LinkedList(); for(int j = 0;j<result.get(level).size();++j)// level will ++ in the behind; we only get the "former" level { Node n = result.get(level).get(j); if(n.left_child!=null) ll2.add(n.left_child); if(n.right_child!=null) ll2.add(n.right_child); } level++; if(ll2.size()>0) { result.add(level,ll2); } else {// if the ll2.size <= 0 that is means we reach the lowest-level-leaves so we break the while(true) loop break; } } return result; }
test function ::
ArrayList<LinkedList<Node>> findLinkedLeve = mygetLevelNode(root); for(LinkedList<Node> n:findLinkedLeve) { System.out.println("same linked list"); for(int i = 0;i<n.size();++i) { Node temp = n.get(i); System.out.print(temp.data+" -> "); } }
相关文章推荐
- [CrackCode] 4.4 Creates a linked list of all the nodes at each depth of a given tree
- Q 4.4 creates a linked list of all the nodes at each depth
- List the maximum or minimum of the elements at each position of two arrays
- Retrieving the list of items in a directory all at once
- Insert at the front of a linked list
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
- The realization of a simple single linked list【1.8】
- Question[SQL]: How can I list all book with prices greather than the average price of books of the same type?
- 用链表实现带菜单功能的一元多项式的基本操作 (Use linked list to realize the basic operation of a one-dimensional polynom)
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- Find the first intersection of two lists (if the two list are merged at some point)
- Delete Node in the Middle of Singly Linked List
- Find the middle point of the linked list
- 【11】Delete a node in the middle of a single linked list
- Find the starting point of a linked list
- Give the common ancestor of all the deepest nodes of a tree
- List All Of The Subset In Another Method
- List All Of The Subset In Another Method
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
- The realization of a simple single linked list【1.6】