在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A binary tree is univalued if every node in the tree has the same value. Return Example 1: Input: [1,1,1,1,1,null,1]
Output: true
Example 2: Input: [2,2,2,5,2]
Output: false
Note:
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。 只有给定的树是单值二叉树时,才返回
示例 1: 输入:[1,1,1,1,1,null,1] 输出:true 示例 2: 输入:[2,2,2,5,2] 输出:false 提示:
12 ms 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 var s:Set<Int> = Set<Int>() 16 func isUnivalTree(_ root: TreeNode?) -> Bool { 17 s.insert(root!.val) 18 if root?.left != nil 19 { 20 isUnivalTree(root!.left) 21 } 22 if root?.right != nil 23 { 24 isUnivalTree(root!.right) 25 } 26 return s.count == 1 27 } 28 } 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 isUnivalTree(_ root: TreeNode?) -> Bool { 16 guard let root = root else { return false } 17 return univalTreeHelper(root, val: root.val) 18 } 19 20 func univalTreeHelper(_ root: TreeNode?, val: Int) -> Bool { 21 guard let root = root else { return true } 22 var matches = root.val == val ? true : false 23 return matches && univalTreeHelper(root.left, val: val) && univalTreeHelper(root.right, val: val) 24 } 25 } 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 isUnivalTree(_ root: TreeNode?) -> Bool { 16 if root == nil { 17 return true 18 } 19 return isUnivalTree(root!, root!.val) 20 } 21 22 func isUnivalTree(_ root: TreeNode?, _ value: Int) -> Bool { 23 if root == nil { 24 return true 25 } 26 27 if (root?.val != value) { 28 return false 29 } 30 31 return isUnivalTree(root?.left, value) && isUnivalTree(root?.right, value) 32 } 33 } 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 isUnivalTree(_ root: TreeNode?) -> Bool { 16 if root == nil { 17 return true 18 } 19 20 if root?.left != nil && root?.val != root?.left?.val { 21 return false 22 } 23 24 if root?.right != nil && root?.val != root?.right?.val { 25 return false 26 } 27 28 return isUnivalTree(root?.left) && isUnivalTree(root?.right) 29 } 30 } 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 isUnivalTree(_ root: TreeNode?) -> Bool { 16 guard let root = root else { return false } 17 return univalTreeHelper(root, val: root.val) 18 } 19 20 func univalTreeHelper(_ root: TreeNode?, val: Int) -> Bool { 21 guard let root = root else { return true } 22 var matches = root.val == val ? true : false 23 return matches && univalTreeHelper(root.left, val: val) && univalTreeHelper(root.right, val: val) 24 } 25 }
|
请发表评论