LeetCode - Univalued Binary Tree

2019-07-17  本文已影响0人  Andy1944

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
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读