[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

➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。

➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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:

[Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

Input: [1,1,1,1,1,null,1]
Output: true

Example 2:

[Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree
Input: [2,2,2,5,2]
Output: false

Note:

  1. The number of nodes in the given tree will be in the range [1, 100].
  2. Each node's value will be an integer in the range [0, 99].

如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。

只有给定的树是单值二叉树时,才返回 true;否则返回 false

示例 1:

[Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

输入:[1,1,1,1,1,null,1]
输出:true

示例 2:

[Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

输入:[2,2,2,5,2]
输出:false

提示:

  1. 给定树的节点数范围是 [1, 100]
  2. 每个节点的值都是整数,范围为 [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 }