★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10180918.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <--- / 2 3 <--- 5 4 <---
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 <--- / 2 3 <--- 5 4 <---
12ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 if let root = root { 17 var result: [Int] = [] 18 var current: [TreeNode] = [root] 19 var next: [TreeNode] = [] 20 21 while !current.isEmpty { 22 if (current.count == 1) { 23 result.append(current[0].val) 24 } 25 26 let node = current.removeFirst() 27 if let left = node.left { 28 next.append(left) 29 } 30 if let right = node.right { 31 next.append(right) 32 } 33 34 if (current.count == 0) { 35 current = next 36 next = [] 37 } 38 } 39 40 return result 41 } else { 42 return [] 43 } 44 } 45 }
16ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 let count = deep(root) 17 18 var res = Array(repeating: 0, count: count) 19 20 rightSideViewHelp(root, 0, &res) 21 return res 22 } 23 24 func deep(_ root : TreeNode?) -> Int { 25 if root == nil { 26 return 0 27 } 28 29 return max(deep(root?.left), deep(root?.right)) + 1 30 } 31 32 func rightSideViewHelp(_ root: TreeNode?, _ row : Int, _ res : inout [Int]){ 33 if root == nil { 34 return 35 } 36 37 res[row] = root!.val 38 rightSideViewHelp(root?.left, row+1, &res) 39 rightSideViewHelp(root?.right, row+1, &res) 40 } 41 }
16ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 17 var res = [Int]() 18 guard var cur = root else { return res } 19 var q = [cur] 20 while !q.isEmpty { 21 var len = q.count 22 for i in 0..<len { 23 cur = q.removeFirst() 24 if i == len-1 { res.append(cur.val) } 25 if let left = cur.left { q.append(left) } 26 if let right = cur.right { q.append(right) } 27 } 28 } 29 return res 30 } 31 }
20ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 var solution = [Int]() 17 _rightSideView(root, &solution, 0) 18 return solution 19 } 20 21 func _rightSideView(_ root: TreeNode?, _ solution: inout [Int], _ level: Int) { 22 guard let root = root else { return } 23 24 if solution.count == level { 25 solution.append(root.val) 26 } 27 _rightSideView(root.right, &solution, level + 1) 28 _rightSideView(root.left, &solution, level + 1) 29 } 30 31 func rightSideViewBad(_ root: TreeNode?) -> [Int] { 32 guard let root = root else { return [] } 33 34 var solution = [LeveledNode]() 35 var stack = [LeveledNode(node: root, level: 0)] 36 37 while !stack.isEmpty { 38 let next = stack.removeFirst() 39 40 if let last = solution.last, last.level == next.level { 41 solution.removeLast() 42 } 43 solution.append(next) 44 45 if let left = next.node.left { 46 stack.append(LeveledNode(node: left, level: next.level + 1)) 47 } 48 if let right = next.node.right { 49 stack.append(LeveledNode(node: right, level: next.level + 1)) 50 } 51 } 52 53 return solution.map { $0.node.val } 54 } 55 } 56 57 struct LeveledNode { 58 let node: TreeNode 59 let level: Int 60 }
20ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 17 var values = [Int].init() 18 19 func getTreeNodeValue(_ node: TreeNode?, index: Int) { 20 21 guard let treeNode = node else { 22 return 23 } 24 25 if index < values.count { 26 values[index] = treeNode.val 27 } else { 28 values.append(treeNode.val) 29 } 30 31 if let left = treeNode.left { 32 getTreeNodeValue(left, index: index + 1) 33 } 34 35 if let right = treeNode.right { 36 getTreeNodeValue(right, index: index + 1) 37 } 38 } 39 40 getTreeNodeValue(root, index: 0) 41 42 return values 43 } 44 }
24ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 guard let root = root else { return [Int]() } 17 var matrix = [[Int]]() 18 var array1 = [TreeNode]() 19 var array2 = [TreeNode]() 20 array1.append(root) 21 while array1.count > 0 { 22 var temp = [Int]() 23 for node in array1 { 24 temp.append(node.val) 25 if let left = node.left { 26 array2.append(left) 27 } 28 if let right = node.right { 29 array2.append(right) 30 } 31 } 32 matrix.append(temp) 33 array1 = array2 34 array2.removeAll() 35 } 36 return matrix.map { array in 37 return array.last! 38 } 39 } 40 }
28ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 rightSide(root, 1) 17 return right 18 } 19 var depth = 0 20 var right = [Int]() 21 func rightSide(_ root: TreeNode?, _ n: Int) { 22 if root != nil { 23 if n > depth { 24 right.append(Int(root!.val)) 25 } 26 if root?.left == nil && root?.right == nil { 27 if n > depth { 28 depth = n 29 } 30 } 31 rightSide(root?.right, n + 1) 32 rightSide(root?.left, n + 1) 33 } 34 } 35 }
36ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 guard let root = root else { 17 return [] 18 } 19 20 var result: [Int] = [] 21 result.append(root.val) 22 23 let left = rightSideView(root.left) 24 let right = rightSideView(root.right) 25 var nums: [Int]! 26 if right.count >= left.count { 27 nums = right 28 } else { 29 if right.isEmpty { 30 nums = left 31 } else { 32 nums = right + left[right.count..<left.count] 33 } 34 } 35 result.append(contentsOf: nums) 36 return result 37 } 38 }
40ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func rightSideView(_ root: TreeNode?) -> [Int] { 16 guard let _ = root else { 17 return [] 18 } 19 20 var nodes: [[TreeNode]] = [[root!]] 21 var res: [Int] = [root!.val] 22 var idx = 0 23 while idx < nodes.count { 24 25 var levelNodes: [TreeNode] = [] 26 for node in nodes.last! { 27 if let _ = node.left { 28 levelNodes.append(node.left!) 29 } 30 if let _ = node.right { 31 levelNodes.append(node.right!) 32 } 33 } 34 if levelNodes.count > 0 { 35 nodes.append(levelNodes) 36 res.append(levelNodes.last!.val) 37 } 38 39 idx += 1 40 } 41 42 return res 43 } 44 }