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:
- The number of nodes in the given tree will be in the range
[1, 100]
. - Each node's value will be an integer in the range
[0, 99]
.
Recursive:
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func isUnivalTree(_ root: TreeNode?) -> Bool {
guard let root else { return true }
if let left = root.left, root.val != left.val {
return false
}
if let right = root.right, root.val != right.val {
return false
}
return isUnivalTree(root.left) && isUnivalTree(root.right)
}
}
Iterative:
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func isUnivalTree(_ root: TreeNode?) -> Bool {
guard let root else { return true }
var stack: [TreeNode] = [root]
while !stack.isEmpty {
let node: TreeNode = stack.removeLast()
if node.val != root.val {
return false
} else {
if let left = node.left {
stack.append(left)
}
if let right = node.right {
stack.append(right)
}
}
}
return true
}
}