You are given the root
of a binary tree containing exactly three nodes: the root, its left child, and its right child.
Return true
if the value of the root
is equal to the sum of the values of its two children, or false
otherwise.
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def checkTree(self, root: TreeNode) -> bool:
return root.val == (root.left.val + root.right.val)
TreeNode
where we define the basic structure of the nodes of the binary tree.checkTree
that takes a root node of the binary tree.true
if the condition is met, otherwise return false
.With the problem constraints and guarantees, this solution efficiently checks the condition in constant time and space.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?