在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree 1 / \ 2 2 / \ / \ 3 4 4 3
But the following 1 / \ 2 2 \ \ 3 3
Note:
给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 1 / \ 2 2 \ \ 3 3 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。 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 isSymmetric(_ root: TreeNode?) -> Bool { 16 return checkTree(root, root) 17 } 18 19 20 private func checkTree(_ left: TreeNode?, _ right: TreeNode?) -> Bool { 21 if left == nil || right == nil { return left == nil && right == nil } 22 23 if left?.val != right?.val { return false } 24 25 return checkTree(left?.left, right?.right) && checkTree(left?.right, right?.left) 26 } 27 } 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 isSymmetric(_ root: TreeNode?) -> Bool { 16 return isSymmetric2(root) 17 18 guard let root = root else { return true } 19 20 var queue: [TreeNode] = [] 21 if let left = root.left { 22 queue.append(left) 23 } 24 if let right = root.right { 25 queue.append(right) 26 } 27 28 while !queue.isEmpty { 29 if queue.count % 2 != 0 { 30 return false 31 } 32 let left = queue.removeFirst() 33 let right = queue.removeFirst() 34 35 if left.val != right.val { 36 return false 37 } 38 if left.left?.val != right.right?.val { 39 return false 40 } 41 if left.right?.val != right.left?.val { 42 return false 43 } 44 45 if left.left != nil { 46 queue.append(left.left!) 47 } 48 if right.right != nil { 49 queue.append(right.right!) 50 } 51 if left.right != nil { 52 queue.append(left.right!) 53 } 54 if right.left != nil { 55 queue.append(right.left!) 56 } 57 } 58 return true 59 } 60 61 func isSymmetric2(_ root: TreeNode?) -> Bool { 62 return _isSymmetric(root, root) 63 } 64 65 func _isSymmetric(_ left: TreeNode?, _ right: TreeNode?) -> Bool { 66 if left == nil || right == nil { 67 return left == nil && right == nil 68 } 69 if left?.val != right?.val { 70 return false 71 } 72 return _isSymmetric(left?.left, right?.right) && 73 _isSymmetric(left?.right, right?.left) 74 } 75 } 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 15 // create a matrix at each depth using a queue and bfs 16 // walk through the levels to see if they are symmetric 17 18 19 class Solution { 20 21 func isSymmetric(_ root: TreeNode?) -> Bool { 22 guard let root = root else { return true } 23 var queue = [root] 24 var tree: [[Int?]] = [[root.val]] 25 26 while queue.count > 0 { 27 28 var newL = [Int?]() 29 30 for _ in 0..<queue.count { 31 let next = queue.first! 32 queue.remove(at: 0) 33 newL.append(next.left?.val) 34 newL.append(next.right?.val) 35 if let l = next.left { 36 queue.append(l) 37 } 38 if let r = next.right { 39 queue.append(r) 40 } 41 } 42 43 tree.append(newL) 44 } 45 46 for i in tree { 47 var level = i 48 while level.count > 1 { 49 let first = level.first! 50 let last = level.popLast() 51 level.remove(at: 0) 52 if first != last { 53 return false 54 } 55 } 56 } 57 58 return true 59 } 60 } 32ms 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 isSymmetricTree(_ left: TreeNode?, _ right: TreeNode?) -> Bool { 16 if left == nil && right == nil { 17 return true 18 } 19 20 if left == nil || right == nil { 21 return false 22 } 23 if left!.val == right!.val { 24 return isSymmetricTree(left?.left,right?.right) && isSymmetricTree(left?.right,right?.left) 25 } 26 return false 27 } 28 29 func isSymmetric(_ root: TreeNode?) -> Bool { 30 if root == nil { 31 return true 32 } 33 return isSymmetricTree(root?.left, root?.right) 34 } 35 } 32ms 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 isSymmetric(_ root: TreeNode?) -> Bool { 16 return isSymmetric(root?.left, root?.right) 17 } 18 19 func isSymmetric(_ node1: TreeNode?, _ node2: TreeNode?) -> Bool { 20 if node1 == nil && node2 == nil { 21 return true 22 } else if node1 == nil && node2 != nil { 23 return false 24 } else if node1 != nil && node2 == nil { 25 return false 26 } 27 28 guard let node1 = node1, let node2 = node2 else { 29 return false 30 } 31 32 if node1.val != node2.val { 33 return false 34 } 35 36 let rightLeftTreeValidation = isSymmetric(node1.right, node2.left) 37 if !rightLeftTreeValidation { 38 return false 39 } 40 41 let leftRightTreeValidation = isSymmetric(node1.left, node2.right) 42 return leftRightTreeValidation 43 } 44 } 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 8 * public init(_ val: Int) { 9 * self.val = val 10 * self.left = nil 11 * self.right = nil 12 * } 13 * } 14 */ 15 class Solution { 16 func isSymmetric(_ root: TreeNode?) -> Bool { 17 return symmetric(root?.left, root?.right) 18 } 19 func symmetric(_ left : TreeNode? , _ right : TreeNode?)-> Bool{ 20 switch (left, right) { 21 case let(left?, right?): 22 return left.val == right.val && symmetric(left.left, right.right) && symmetric(left.right, right.left) 23 case (nil, nil): 24 return true 25 default: 26 return false 27 } 28 } 29 }
|
请发表评论