一招鲜吃遍天= =。。
func rightSideView(_ root: TreeNode?) -> [Int] {
if root == nil {
return []
}
var array = Array<TreeNode>()
var levArray = Array<Int>()
levArray.append((root?.val)!)
array.append(root!)
var count = 1
while !array.isEmpty {
let node = array.removeFirst()
count -= 1
if let left = node.left {
array.append(left)
}
if let right = node.right {
array.append(right)
}
if count == 0 {
count = array.count
if array.count > 0{
levArray.append(array.last!.val)
}
}
}
return levArray
}
网友评论