zoukankan      html  css  js  c++  java
  • Daily Coding Problem: Problem #994

    import java.util.*
    
    /**
     * This problem was asked by Microsoft.
     * Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5.
      1
     / 
    2   3
       / 
      4   5
     * */
    class Problem_994 {
        /*
        * solution: BFS, Time:O(n), Space:O(n)
        * */
        fun printLevel(node: Node) {
            val queue = LinkedList<Node>()
            queue.offer(node)
            while (queue.isNotEmpty()) {
                //pop from head
                val cur = queue.pop()
                //println it out
                println(cur.value)
                //add into tail
                if (cur.left != null) {
                    queue.offer(cur.left)
                }
                //add into tail
                if (cur.right != null) {
                    queue.offer(cur.right)
                }
            }
        }
    }
  • 相关阅读:
    方法
    Go中的OOP
    GO 结构体
    指针
    闭包
    回调函数
    匿名函数
    函数的数据类型及本质
    defer语句
    递归函数
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15257561.html
Copyright © 2011-2022 走看看