2017-12-11

Posted yunyouhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-12-11相关的知识,希望对你有一定的参考价值。

242. Convert Binary Tree to Linked Lists by Depth 

Given a binary tree, design an algorithm 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).

*dummy node的使用,中间node和dummy node的关系。

技术分享图片
 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  * Definition for singly-linked list.
12  * public class ListNode {
13  *     int val;
14  *     ListNode next;
15  *     ListNode(int x) { val = x; }
16  * }
17  */
18 public class Solution {
19     /**
20      * @param root the root of binary tree
21      * @return a lists of linked list
22      */
23     public List<ListNode> binaryTreeToLists(TreeNode root) {
24         // Write your code here
25         List<ListNode> res = new ArrayList<>();
26         if (root == null) {
27             return res;
28         }
29         Queue<TreeNode> queue = new LinkedList<>();
30         queue.add(root);
31         ListNode head = new ListNode(0);
32         while (!queue.isEmpty()) {
33             int size = queue.size();
34             ListNode ln = head;
35             for (int i = 0; i < size; i++) {
36                 TreeNode node = queue.poll();
37                 ln.next = new ListNode(node.val);
38                 ln = ln.next;
39                 if (node.left != null) {
40                     queue.add(node.left);
41                 }
42                 if (node.right != null) {
43                     queue.add(node.right);
44                 }
45             }
46             res.add(head.next);
47         }
48         return res;
49     }
50 }
View Code

 

以上是关于2017-12-11的主要内容,如果未能解决你的问题,请参考以下文章

一对一培训之视频免费分享-2017-12-11-第 01 阶段-规划

2017-12-11 1周1次课

2017.12.11SimpleDateFormat的线程安全性讨论

2017.12.11 1周1次课

涉及日期 fns 格式()的时区问题

linux 2017-12-11 第1周第1次课 学习笔记