[Swift]LeetCode965. 單值二叉樹 | Univalued Binary Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-egwxefzb-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

A binary tree is univalued if every node in the tree has the same value.node

Return true if and only if the given tree is univalued. git

Example 1:github

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

Example 2:微信

Input: [2,2,2,5,2]
Output: false

Note:spa

  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].

若是二叉樹每一個節點都具備相同的值,那麼該二叉樹就是單值二叉樹。code

只有給定的樹是單值二叉樹時,才返回 true;不然返回 falsehtm

 

示例 1:blog

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

示例 2:get

輸入:[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 }
相關文章
相關標籤/搜索