Skip to content

Latest commit

 

History

History
66 lines (54 loc) · 1.25 KB

107-Binary-Tree-Level-Order-Traversal-II.md

File metadata and controls

66 lines (54 loc) · 1.25 KB

107. Binary Tree Level Order Traversal II

2017-03-22

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example: Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.left = nil
 *         self.right = nil
 *     }
 * }
 */
class Solution {
    func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
        var res = [[Int]]()
        guard let n = root else { return res }
        var layer = [n]
        while layer.count > 0 {
            var temp = [Int]()
            for i in 0..<layer.count {
                let n = layer.removeFirst()
                temp.append(n.val)
                if let l = n.left { layer.append(l) }
                if let r = n.right { layer.append(r) }
            }
            res.insert(temp, at: 0)
        }
        return res
    }
}