//
//  102_BinaryTreeLevelOrderTraversal.swift
//  HRSwift
//
//  Created by yansong li on 2016-08-04.
//  Copyright © 2016 yansong li. All rights reserved.
//

import Foundation

/**
  Swift Knowledge:
 
  Queue, Array
 
  Algorithem Knowledge:
 
  BFS
 */

/**
  102. Binary Tree Level Order Traversal
  https://leetcode.com/problems/binary-tree-level-order-traversal/
 */

class BinaryTreeLevelOrderTraversal_Solution {
  func levelOrder(_ root: TreeNode?) -> [[Int]] {
    guard let node = root else {
        return []
    }
    var result: [[Int]] = []
    var queue = Queue<(TreeNode, Int)>()
    queue.enqueue((node, 0))
    while !queue.isEmpty() {
        let (currentNode, currentLevel) = queue.dequeue()!
        if currentLevel >= result.count {
            result.append([])
        }
        result[currentLevel].append(currentNode.val)
        if let left = currentNode.left {
            queue.enqueue((left, currentLevel + 1))
        }
        if let right = currentNode.right {
            queue.enqueue((right, currentLevel + 1))
        }
    }
    return result
  }
}

//
//  102_BinaryTreeLevelOrderTraversal.swift
//  HRSwift
//
//  Created by yansong li on 2016-08-04.
//  Copyright © 2016 yansong li. All rights reserved.
//

import Foundation

/**
  Swift Knowledge:
 
  Queue, Array
 
  Algorithem Knowledge:
 
  BFS
 */

/**
  102. Binary Tree Level Order Traversal
  https://leetcode.com/problems/binary-tree-level-order-traversal/
 */

class BinaryTreeLevelOrderTraversal_Solution {
  func levelOrder(_ root: TreeNode?) -> [[Int]] {
    guard let root = root else {
      return []
    }
    var queue = Queue<(TreeNode, Int)>()
    var ans: [[Int]] = [[]]
    queue.enqueue((root, 1))
    while !queue.isEmpty() {
      let (currentNode, currentlevel) = queue.dequeue()!
      if ans.count == currentlevel {
        var lastLevelNodes = ans.removeLast()
        lastLevelNodes.append(currentNode.val)
        ans.append(lastLevelNodes)
      } else if ans.count < currentlevel {
        let newLevelNodes = [currentNode.val];
        ans.append(newLevelNodes)
      }
      if let left = currentNode.left {
        queue.enqueue((left, currentlevel + 1))
      }
      if let right = currentNode.right {
        queue.enqueue((right, currentlevel + 1))
      }
    }
    return ans
  }
}

--- 
+++ 
@@ -25,29 +25,25 @@
 
 class BinaryTreeLevelOrderTraversal_Solution {
   func levelOrder(_ root: TreeNode?) -> [[Int]] {
-    guard let root = root else {
-      return []
+    guard let node = root else {
+        return []
     }
+    var result: [[Int]] = []
     var queue = Queue<(TreeNode, Int)>()
-    var ans: [[Int]] = [[]]
-    queue.enqueue((root, 1))
+    queue.enqueue((node, 0))
     while !queue.isEmpty() {
-      let (currentNode, currentlevel) = queue.dequeue()!
-      if ans.count == currentlevel {
-        var lastLevelNodes = ans.removeLast()
-        lastLevelNodes.append(currentNode.val)
-        ans.append(lastLevelNodes)
-      } else if ans.count < currentlevel {
-        let newLevelNodes = [currentNode.val];
-        ans.append(newLevelNodes)
-      }
-      if let left = currentNode.left {
-        queue.enqueue((left, currentlevel + 1))
-      }
-      if let right = currentNode.right {
-        queue.enqueue((right, currentlevel + 1))
-      }
+        let (currentNode, currentLevel) = queue.dequeue()!
+        if currentLevel >= result.count {
+            result.append([])
+        }
+        result[currentLevel].append(currentNode.val)
+        if let left = currentNode.left {
+            queue.enqueue((left, currentLevel + 1))
+        }
+        if let right = currentNode.right {
+            queue.enqueue((right, currentLevel + 1))
+        }
     }
-    return ans
+    return result
   }
 }
Update 102 Binary Tree Level Order Traversal.
