• 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

    531. Six Degrees

    问题:List<UndirectedGraphNode> graph输入参数意义?

    step错误,忘记return

  • 相关阅读:
    PDO扩展
    阿里云ECS VSFTP上传本地文件
    Nginx+lua_Nginx+GraphicsMagick来实现实时缩略图
    Mysql 5.6主从同步配置与解决方案
    windows安装配置mongodb及图形工具MongoVUE
    安装phpredis扩展以及phpRedisAdmin工具
    Redis安装配置以及开机启动
    CentOS安装Git服务器 Centos 6.5 + Git 1.7.1.0 + gitosis
    OpenStack 入门3
    Openstack 入门2
  • 原文地址:https://www.cnblogs.com/yunyouhua/p/8025444.html
Copyright © 2020-2023  润新知