美文网首页
LeetCode - Univalued Binary Tree

LeetCode - Univalued Binary Tree

作者: Andy1944 | 来源:发表于2019-07-17 18:24 被阅读0次

    Univalued Binary Tree - LeetCode

    Solution

    class Solution {
        func isUnivalTree(_ root: TreeNode?) -> Bool {
            if let root = root {
                return isUnivalTree(root, root.val)
            } else {
                return true
            }
        }
        
        func isUnivalTree(_ root: TreeNode?, _ val: Int) -> Bool {
            if let root = root {
                if val == root.val && isUnivalTree(root.left, val) && isUnivalTree(root.right, val) {
                    return true
                } else {
                    return false
                }
            } else {
                return true
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:LeetCode - Univalued Binary Tree

          本文链接:https://www.haomeiwen.com/subject/pnmclctx.html