[Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10201416.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
A binary tree is univalued if every node in the tree has the same value.
Return true
if and only if the given tree is univalued.
Example 1:
Input: [1,1,1,1,1,null,1]
Output: true
Example 2:

Input: [2,2,2,5,2]
Output: false
Note:
- The number of nodes in the given tree will be in the range
[1, 100]
. - Each node's value will be an integer in the range
[0, 99]
.
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true
;否则返回 false
。
示例 1:
输入:[1,1,1,1,1,null,1] 输出:true
示例 2:
输入:[2,2,2,5,2] 输出:false
提示:
- 给定树的节点数范围是
[1, 100]
。 - 每个节点的值都是整数,范围为
[0, 99]
。
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了