• [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:

    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:

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

    示例 2:

    输入:[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 }
  • 相关阅读:
    MVP Summit 2008 照片纪实(三) 922公里,目标拉斯维加斯
    Cool 给你的Visual Studio 添加音乐
    DB2 9 使用拓荒(733 考试)认证指南,第 4 部分: 嵌入式 SQL 编程(2)
    DB2 9 运用开拓(733 考试)认证指南,第 3 部分: XML 数据行使(6)
    DB2 9 运用开辟(733 考试)认证指南,第 3 部门: XML 数据独霸(4)
    DB2 9 运用开发(733 考试)认证指南,第 3 部门: XML 数据把持(8)
    DB2 9 运用开拓(733 检修)认证指南,第 6 部分: .NET 编程(3)
    DB2 9 操纵开辟(733 测验)认证指南,第 4 局部: 嵌入式 SQL 编程(5)
    DB2 9 使用启示(733 检验)认证指南,第 3 部分: XML 数据把持(5)
    DB2 9 使用拓荒(733 检验)认证指南,第 4 部分: 嵌入式 SQL 编程(3)
  • 原文地址:https://www.cnblogs.com/strengthen/p/10201416.html
Copyright © 2020-2023  润新知